Static - Java
When a member is declared static it can be accessed before any objects of its classes are created. We can declare both methods and variables to the static.
Static methods have several restrictions. They are
- They can only call other static methods.
- They must only access static data.
- They can’t refer to ‘this’ or ‘super’ in anyway.
We can declare the static block which gets executed exactly once when the class is first loaded.
WRITE A PROGRAM ON STATIC METHODS.
class Static_sample
{
static int a=10;
static int b=20;
static int c;
static public void get(int x)
{
System.out.println(x);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
static
{
System.out.println("Static block");
c=c+b;
}
public static void main(String args[])
{
get(10);
}
}
WRITE A PROGRAM ON SATIC METHODS.
class Stat
{
static int a=10;
static int b=20;
static public void get(int x)
{
System.out.println(x);
System.out.println(a);
System.out.println(b);
}
}
class Sample
{
public static void main(String args[])
{
Stat.get(10);
System.out.println("a="+ Stat.a);
System.out.println("b="+ Stat.b);
}
}
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743