The document discusses the static keyword in Java. It explains that static members belong to the class rather than objects, and are loaded when the class is loaded. Static variables and methods can be accessed without creating an object. Constructors are used to initialize non-static variables when an object is created. Static blocks run when the class is loaded and can initialize static variables.
3. how objects are created?
Basic Steps of how objects are created
1. Class is loaded by JVM
2. Static variable and methods are loaded and initialized
and available for use
3. Constructor is called to instantiate the non static variables
4. Non static variables and methods are now available
• As all the non static variable are
available only after the constructor is
called, there is a restriction on using
non static variable in static methods.
4. what’s the use of static Keyword and
Why?
The static keyword is used when a member variable of a class has to be
shared between all the instances of the class.
All static variables and methods belong to the class and not to any
instance of the class
Why ?
The static keyword in java is used for memory management mainly.
Static keyword is used to save your time and typing .
5. Static and Non-static
We can access static variables without creating an object of the class
As they are already available at class loading time.
We cannot use non static methods and variables without creating an
object of the class as they are bound to the instance of the class.
They are initialized by the constructor when we create the object using
new operator.
6. The static can be:
static
Variables Methods Class Blocks Constructor!
7. Static Variables
The static variable can be used to refer the common property of all objects
(that is not unique for each object).
The static variable gets memory only once in class area at the time of class
loading.
Advantage of static variable :-
It makes your program memory efficient (i.e. it saves memory).
Student8.java
Counter.java
9. Static Methods
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance
of a class.
static method can access static data member and can change the value
of it.
Student9.java
Calculate.java
10. Static Classes (Only Nested Class)
A Class can be made static only if it is a nested Class.
The nested static class can be accessed without having an object of outer
class.
StClass.java
11. Static Blocks
Static block is mostly used for changing the default values of static
variables.
This block gets executed when the class is loaded in the memory.
A class can have multiple Static blocks, which will execute in the same
sequence in which they have been written into the program.
Stblocks.java
Stblocks2.java
//Program of static variable
class Student8 {
int rollno;
String name;
static String college ="ITS";
Student8(int r,String n)
{
rollno = r;
name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+college);
}
public static void main(String args[])
{
Student8 s1 = new Student8(111,"Karan");
Student8 s2 = new Student8(222,"Aryan");
System.out.println(college);
s1.display();
s2.display();
}
}
=======================================class Counter{
static int count=0;//will get memory when instance is created and try without static to see the difference
Counter(){
count++;
System.out.println(count);
}
public static void main(String args[]){
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
//Program of changing the common property of all objects(static field).
class Student9{
int rollno;
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student9(int r, String n){
rollno = r;
name = n;
}
static void display (){System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
change();
Student9 s1 = new Student9 (111,"Karan");
Student9 s2 = new Student9 (222,"Aryan");
Student9 s3 = new Student9 (333,"Sonoo");
s1.display();
s2.display();
s3.display();
}
}
=====================================
//Program to get cube of a given number by static method
class Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
}
}
/*A Class can be made static only if it is a nested Class.
//The nested static class can be accessed
without having an object of outer class.*/
class stClass{
//Static class
class X{
String str ="Inside Class X";
}
public static void main(String args[])
{
stClass.X.str=“ahmed";
X.str="Inside Class Example1";
System.out.println("String stored in str is- "+ X.str);
}
}
class Stblocks{
static int num=5;
static String mystr;
static{
num = 97;
mystr = "Static keyword in Java";
}
public static void main(String args[])
{
System.out.println("Value of num="+num);
System.out.println("Value of mystr="+mystr);
}
}
===========================================
class Stblocks2{
static int num;
static String mystr;
//First Static block
static{
System.out.println("Static Block 1");
num = 68;
mystr = "Block1";
}
//Second static block
static{
System.out.println("Static Block 2");
num = 98;
mystr = "Block2";
}
public static void main(String args[])
{
System.out.println("Value of num="+num);
System.out.println("Value of mystr="+mystr);
}
}