Thursday, August 19, 2021

# 8. static and non-static variables

 45 is a data 

According to java, this data must be there in a container called variable

Which one do we take 

This 45 should be there in static or nonstatic or local or a reference type variable?

Static variables (class level):

Q) When do we go for a static variable.

Whenever the data of a variable is not going to be changed/ constant from object to object then go for static variables

Q) When do we go for non-static variables?

Whenever the data is going to be changed/ not constant from object to object then go for non-static variables.

case1: 45 is a data going to be changed then go for non-static

case2: 45 is a data is not going to be changed then go for static

For example: 

Assume that in a classroom we have 35 people, 

for all the student's institute names is going to change or not?

Static

For all these students trainer name is going to change or not

Static

Student name is going to change or not?

Yes from student to student

Non-static

Daily topics are going to change or not?

Yes

Non-static

A number of hands from person to person is going to change or not?

Not going to be changes

Static

Legs?

Not going to be changes

Static

Color?

Going to be changes

Non-static variable

Person phone number?

Going to changes

Non-static

Q) How to define static variables

Syntax:

<access specifier>      static         data-type       variable-name    =     value/data ;

-----------------------------------------------------------------------            -----------------

declaration                                                                                            initialization

Ex: public static int numberOne = 45;

 static    <access specifier>       data-type        variable-name       =     value/data ;

Ex: static public int number_One = 45 ;

We have done declaration and initialization in a single line

 static    <access specifier>       data-type        variable-name ;   

variable-name       =     value/data ;

Ex: static public int number_One;

number_One = 45 ;

Here Declaration is done in one line and initialization is done in another line.


Example:

public class Static_Program{        // access specifier, class keyword, class name, start of the class

// declaration and initialization in same line

public static int num1=20;        // access specifier, data is constant, data type, variable name, data

static public int num2=25;        // data is constant, access specifier, data type, variable name, data

static int num4=30;        // data is constant, access specifier is optional, data type, variable name,data

// declaration and initialization in different lines

static public int num3;        //declaration,data is constant, access specifier, data type, variable name

num3=80;        // initialization, variable name, data

}        //end of the class










No comments:

Post a Comment

# 16. control statements

 Control statements: In java, by using control statements we can tell the flow of execution. in which order the statements should execute. d...