We would see following type of variables in
Java:
- Local Variables
- Global variables
Local Variables : - A variable that is declared inside the method or block is called local variable. Local variables within
the method or block others methods or block can’t be used .There is no default
values
public class localVariables
{
//local
variables can be declared within the block
static
{
float f;
System.out.println(f); //CTE
f=10;
System.out.println(f);
}
//
iib block
{
double d;
System.out.println(d); //CTE
//local variables can be
declared within the method and should be instilized
d=10.5;
System.out.println(d);
}
//local
variables can be declared within the method
void method1()
{
int u;
System.out.println(u);
//local
variables can be declared within the method and should be instilized
u=20;
System.out.println(u);
//local
variables can't be out side
System.out.println(i); //CTE
}
//
CONSTRUCTOR
LocalVariables()
{
int t;
System.out.println(t); //cte
t=56;
System.out.println(t);
}
public static void main(String[] args)
{
//object creation
localVariables v1=new
localVariables();
int i=10;
System.out.println(i); //10
//local
varibles should be instlized
int j;
System.out.println(j); //CTE
//local varibles should be
used after the instilization only
int k;
int y=k;
System.out.println(y); //
IILEGAL FORWARD REF
//local
varibles any times of reinstilized
i=20;
System.out.println(i);
i=50;
System.out.println(i);
//local variables can't be
out side
system.out.println(u);
// CTE
}
}
Global variables :
Global variables contains default values
its is based on the relevant data types
We can use any where in the class it’s
based on type of global variables.
Class Variables :-
- Class
variables are variables declared with in a class, outside any method, with
the static keyword.
- Whenever
the class loaded into the memory automatically loaded into static variables
before main method
A class can have any number of methods to
access the value of various kinds of methods
Default values for static variables :
public class Defultvalues
{
int i;
String s1;
double d;
long l;
float f;
char c;
boolean b;
byte y;
public static void main(String[] args)
{
defultvalues
v1=new defultvalues();
System.out.println(v1.i); //0
System.out.println(v1.s1); //null
System.out.println(v1.d); //0.0
System.out.println(v1.l); //0
System.out.println(v1.f); //0.0
System.out.println(v1.c); //single space
System.out.println(v1.b); //false
System.out.println(v1.y); //0
}
}
Simple
Program :
public class Staticv
{
//
static varibles declaration
static int i;
static
{
static int h=50; //cte
System.out.println(i); // cts
}
public static void main(String[] args)
{
// static varibles can't be
used in side main method
static double d=10; //cte
System.out.println(i); // 0
}
}
A variable that is declared inside the class but outside the
method is called instance variable . It is not declared as static.
public class Instancevariables
{
// non static varibles
int i=10;
double d=10;
int test1(int i)
{
System.out.println(i);
return i;
}
public static void main(String[] args)
{
int j=10;
Instancevariables ref=new Instancevariables();
// non static varbiles can
be used when ever craeating object
System.out.println(i); // cte if cts i declared as static
System.out.println(ref.i);
System.out.println(ref.j); //cte
ref.i=20;
//test1(ref.j);
//non static members can be
calling with ref of object
ref.test1(10);
}
}
Default values:
In java default some value will be assigned in the variables based on data type
public class Defultvalues
{
int i;
String s1;
double d;
long l;
float f;
char c;
boolean b;
byte y;
public static void main(String[] args)
{
defultvalues v1=new defultvalues();
System.out.println(v1.i); //0
System.out.println(v1.s1); //null
System.out.println(v1.d); //0.0
System.out.println(v1.l); //0
System.out.println(v1.f); //0.0
System.out.println(v1.c); //single space
System.out.println(v1.b); //false
System.out.println(v1.y); //0
}
}
|
Default values
public class Defultvalues
{
int i;
String s1;
double d;
long l;
float f;
char c;
boolean b;
byte y;
public static void main(String[] args)
{
defultvalues
v1=new defultvalues();
System.out.println(v1.i); //0
System.out.println(v1.s1); //null
System.out.println(v1.d); //0.0
System.out.println(v1.l); //0
System.out.println(v1.f); //0.0
System.out.println(v1.c); //single space
System.out.println(v1.b); //false
System.out.println(v1.y); //0
}
}
No comments:
Post a Comment