A class basically contains two sections. One declares variables and other declares methods. These variables and methods values depends on objects of that class. That is, if a class have three object then three copies of data is allocated each of the class member like variables and methods. So, these values are associated with instances of class, then they are called instance variables and instance methods.
For example, consider the following class where it contains two variables and method to display those variables
import java.io.*;
class A
{
int a, b;
void display_ab()
{
System.out.println(" The value of a is=" +a);
System.out.println(" The value of b is=" +b);
}
}
class instance-demo
{
public static void main( String args[])
{
A a1= new A();
a1.a=10;
a1.b=20;
a1.display_ab();
A a2 = new A();
a2.a=30;
a2.b=40;
a2.display_ab();
}
}
The above program will generates the following output
The value of a is = 10
The value of b is = 20
The value of a is = 30
The value of b is = 40
From the above results it is concluded that for each and every object there is a separate copy of the data is created.
If we want to define a member that is common to all the objects and accessed without using a particular object. That is, the member belongs to class as a whole rather than the objects created for the class. Such members are declared using " static" Keyword.
class Test1
{
static int x=10;
static void access()
{
System.out.println(" x = " +x)
}
}
class Demo1
{
public static void main ( String args[])
{
Test1.access();
}
}
The static members are called using class names. In fact, no objects have been created for use. static methods have several restrictions
They can call only other static methods
They can only access static data
They can't refer to this or super keyword in any way.
Some interesting facts about static keyword in java
For example, consider the following class where it contains two variables and method to display those variables
import java.io.*;
class A
{
int a, b;
void display_ab()
{
System.out.println(" The value of a is=" +a);
System.out.println(" The value of b is=" +b);
}
}
class instance-demo
{
public static void main( String args[])
{
A a1= new A();
a1.a=10;
a1.b=20;
a1.display_ab();
A a2 = new A();
a2.a=30;
a2.b=40;
a2.display_ab();
}
}
The above program will generates the following output
The value of a is = 10
The value of b is = 20
The value of a is = 30
The value of b is = 40
From the above results it is concluded that for each and every object there is a separate copy of the data is created.
If we want to define a member that is common to all the objects and accessed without using a particular object. That is, the member belongs to class as a whole rather than the objects created for the class. Such members are declared using " static" Keyword.
class Test1
{
static int x=10;
static void access()
{
System.out.println(" x = " +x)
}
}
class Demo1
{
public static void main ( String args[])
{
Test1.access();
}
}
The static members are called using class names. In fact, no objects have been created for use. static methods have several restrictions
They can call only other static methods
They can only access static data
They can't refer to this or super keyword in any way.
Some interesting facts about static keyword in java
- static is a modifier applicable for variables and methods but not for classes ( but innerclass can be declared as static )
- If the value of a variable is varied from object to object them we should go for instance variable otherwise declare it as static
- static members can access from both instance and static areas where as instance members can be accessed only from instance areas directly i.e, from static area we can't access instance members directly, if we try compiler gives an error.
- for static methods compulsory implementation should be available where as for abstract methods implementation should not be available always. Hence abstract static combination is illegal for methods.
- For static methods overloading concept is applicable.
- static members are executed by java before creating object to that class. Hence they are not available to instance methods.
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.