Static Modifier in Java.

In this article,Static Modifier in Java is explained in detail with examples.
This modifier can be used in four situations
  • Data-members.
  • Member-Methods.
  • Static Block.
  • Classes-Related to nested class.
i)Static data-Members.
These data members are those data members,which are common for all the objects of a class.
Static is the keyword used to declare them.
Ex
static int c;
static String coursename;
static int collegephoneNumber;
static data members of a class can be accessed directly with class name as well as with object also.
Ex
class Student
{
----
---
}
accessed as student.collegecode or s1.collegecode.
However non-static data members can be accessed only with object of a class.
Static data members are allocated only one common sharable memory in ram for all the objects.
Hence static data members are also known as "class-variables". (From topic Static Modifier in Java)
Ex
//program related to Static Modifier in Java--static data members
class Demo1
{
static int a=10;
int b=20;
void CallMe()
{
a++;
b++;
}
void display()
{
System.out.println("A is: "+a);
System.out.println("B is: "+b);
}
}
class StaticDemo
{
public static void main(String args[])
{
Demo1 obj1=new Demo1();
Demo1 obj2=new Demo1();
Demo1 obj3=new Demo1();
Demo1 obj4=new Demo1();
StaticDatamembers-Javaformobj1.CallMe();
obj1.display();
obj2.CallMe();
obj2.display();
obj3.CallMe();                                              
obj3.display();
obj4.CallMe();
obj4.display();
System.out.println("Final value of A is: "+Demo1.a);
System.out.println("Final value of A is: "+obj1.a);
System.out.println("Object1 B value is: "+obj1.b);
System.out.println("Object2 B value is: "+obj2.b);
System.out.println("Object3 B value is: "+obj3.b);
System.out.println("Object4 B value is: "+obj4.b);
//System.out.println("Object1 B value is: "+Demo1.b);
//non-static data-members should be accessed only through objects itself.
}
}
Output
                           To get the code file use me.

ii)Static Methods of class or Member-methods.
It is a method whose implementation is common for all the object of a class.
Static is a keyword used to define a method.
Ex
 static void WriteExam()
{
----
}
static void attendClasses()
{
-----
}
Note
i)static methods of a class, can access only other static members/methods directly but to access non-static memebers, it requires object of a class.
ii)However non-static methods of a class can access both static as well as non-static methods directly.
iii) static methods can be called directly with class name as well as with object name of a class.
Ex
Student.WriteExam(); or s1.WriteExam();
Ex
//program related to Static Modifier in Java--static-members methods
class StaticExample
{
static int i=10;
int j=20;
static void print()
{
int k=30;
System.out.println("I value is: "+i);
//System.out.println("J value is: "+j);
System.out.println("K value is: "+k);
}
void display()
{
System.out.println("I value is: "+i);
System.out.println("J value is : "+j);
}
public static void main(String args[])
{
StaticExample.print();
//System.out.println("J value is: "+j);
StaticExample obj=new StaticExample();
System.out.println("Using object J value is: "+obj.j);
System.out.println();
obj.display();
}
}
Output
StaticExample-Output-Javaform
                             To get the code file use me.

iii) Static Block
It is a block of code in a program,proceeded by the keyword "Static".It is also known as static Initializer.Because this block of code is executed before the main method in a class. (From topic Static Modifier in Java)
Ex
static
{
----
-----
}
static block can access only other static contents of a class directly, same as that of static methods.

Ex
//programs related to Static Modifier in Java--static block
class StaticInitializers
{
static int a=10;
static int b=20;
static void method1(int x)
{
System.out.println("X value is: "+x);
System.out.println("A value is: "+a);
System.out.println("B value is: "+b);
}
static
{
System.out.println("Static Block Initialization");
b=a*4;
a=a+10;
System.out.println("Hello world");
}
public static void main(String args[])
{
System.out.println("Main() Started");
method1(42);
}
}
Output
StaticInitializers-output-Javaform
                            To get the code file use me.

Note
Without writing anything inside the main method,we can print some text on output screen using static block. (From topic Static Modifier in Java)
.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required