Thursday 25 September 2014

Can a class be static in java?

Static class in java :

Static class in java :



YES, we can  have static classes in java like we've got static variables and static method and static things are related to the complete class not with an object of a class.

Similarly ,static classes can even be related to another class. therefore we are able to have solely static nested classes. Static variables and methods
Static variables and methods


Static variables and methods :
Static Variable :Static variables belongs to the complete class instead of one object .
They have only one copy of memory
They can be access with simply name of class ,we don't need an object to initialize or access them
For eg . we've got a class Student as below :
public class Student{
private static String studentId;
public Static void studenttest()
{
}
So we are able to access each static method and variable as
Student.studentId
Student.studenttest()





You may like :
Custom Exception in Java -Tutorial

No comments:

Post a Comment