Friday, August 20, 2021

# 9. non static variables

Q) Where do we need to declare static variables?

 After starting the class immediately declare the static variable.

RAMESHSOFT ====> is a data

public static string Name = "RAMESHSOFT";

If we call that institute Name we will get RAMESHSOFT

In java initialization of the static variables is not mandatory, it is optional.

If we don't declare initialization of static variables then JVM is going to assign the default values

static int number_Two; ===> Valid

public static int number_Two; ===> Valid

Default values:

byte    short    int    long ===> 0

float    double ==========>0.0

String ================> null

boolean ==============> false

static int number_Two;   ===== > 0

static String ifYouDontDoPractice; =====> null

Whenever we declare the variable depends upon the data type for every variable internally some memory is going to be allocated.

Everything in the world occupies some space


Q) How to define non-static variables

Standard syntax:

If there is no static keyword then it will be non-static syntax

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

public int number_Two = 34;

<access specifier>       data-type        variable-name ;   

variable-name       =     value/data ;

public int number_Two ;

number_Two = 34 ;

Access specifier is not mandatory it is an optional


public int id=456; ========> non-static variable

static string target="java"; ==> static variable

double d=456.45; ========> non-static variable

float f1=456.456f; ========> non static variable

static int practiceHrs=9; ===> static variable

Q) Where do we need to declare non-static variables?

After starting the class immediately we need to declare.


If we dont specify initialization of variable then JVM will automatically assign some default value.

Q) Default values applicable for non-static variables?

Yes

byte    short    int    long ===> 0

float    double ==========>0.0

String ================> null

boolean ==============> false.


// Write addition program 45+47=92

// Assume 45 and 47 going to be changes means non-static

public class Addition_Program {

public int number_One=45;

public int number_Two=47;

}


 Example:


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


// declare static variables

// declaration and initialization in single line

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

static int num2=20;        // access specifier is optional, data is constant, data type, variable name, data

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


// declaration and initialization in two lines

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

num4=40;        // variable name, data


// declare non static variables

// declaration and initialization in one line

public int num5=50;        // data changes, access specifier, data type, variable name,data

int num6=60;        // data changes,access specifier is optional, data type, variable name, data

int num7;        // data changes, access specifier is optional, data type ,variable name

num7=70;        // variable name,data


// if we don't initialize then, the default values are going to be assigned by JVM

// No initialization 

// Only declaration

// for static

static public int num8;        //default value for int =0

static float num9;        //default value for float=0.0

static String name;        //default value for String=null


// for non static

public int num9;        //default value for int=0

float num10;        // default value for float=0.0

String name;        // default value for String=null


}        //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...