This is one of the very important topics of core Java. In this
post i am going to explain you about static in Java. First and very
important point is you can access static methods and variables without
creating an object of class. You can access static variables and methods
by using classname.static_member.
Ex.
class StatEx
{
static int i;
void fun()
{
System.out.println(StatEx.i);
}
}
In above example you can notice that i have accessed i variable by using className (StatEx.i). And same you can do with the static member function.
Some of the important features of static ;
Ex.
class StatEx
{
static int i;
void fun()
{
System.out.println(StatEx.i);
}
}
In above example you can notice that i have accessed i variable by using className (StatEx.i). And same you can do with the static member function.
Some of the important features of static ;
- Static members can be accessed without creating an object of class.
- Static function can access only static methods/variables.
- You can initialize static members in static block .It gets executed only once when class loads.
- Static variables also called as class variable.
- Outer class or main class can not be static but you can declare inner class as static.
- Static method can not be overridden.
- Binding happens at compile time.
- In java we have one public static void main method which is also static, an entry point for execution.
No comments:
Post a Comment