static variable static variable in java java static variable example static variable in java class static and non static variables in java example static class variable in java class level static variable in java difference between static and non static variables in java static vs non static variables in java
Static
Variable:
Static variables of
class in which the concept of static has to do with whether
something
is part of a class or an object (instance).
In the case of the main method
which is declared as static, it says that the main
method is an
class method.
A method that is part of a class, not part
of an object. This means that another class
could call a class method of
another class, by referring to the Class Name method.
1) Static variables are those whose memory space is creating only once whenever the
class is loaded into main memory irrespective of number of objects is created.
2) Static
variables are always used for storing common values.
3) Programmatically
static variables declaration must be preceded by static keyword.
Syntax :
Static dataype v1,v2……vn;
4) Each and every static variable must be
accessed w.r.t class name.
Classname.staticvariable;
5) Each
and every value of static variable is sharable.
Class
staticvariableDemo
{
static int
x=10;
static void
fun1()
{
System.out.println(“this is static function”);
}
}
Class oneclass
{
void fun2();
{
staticDemo.fun1();
System.out.println(“The value of x”+staticDemo.x);// calling static data member using
//class name
}
public static
void main(String args[])
{
oneclass obj=new
oneclass();
obj.fun2();
}
}
No comments:
Post a Comment