0% found this document useful (0 votes)
19 views2 pages

Static Keyword Notes

The static keyword in Java is used for memory management and belongs to the class rather than its instances. It can be applied to variables, methods, blocks, and nested classes, allowing shared properties across all objects. Static variables are allocated memory once at class loading and can be accessed without creating an instance of the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Static Keyword Notes

The static keyword in Java is used for memory management and belongs to the class rather than its instances. It can be applied to variables, methods, blocks, and nested classes, allowing shared properties across all objects. Static variables are allocated memory once at class loading and can be accessed without creating an instance of the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Static Keyword Notes:

Static Keyword belongs to the class rather than the instance of the class.It is mainly use for memory
management. Static member access and alive before and after the instance creation.Static is used
with the variable,blocks,methods and nested classes.

Static used with:

1.Variables.

2.Methods

3.Block

4.Nested classes

1.Variables:

If you declare any variable static,It is called static variable. Java static property is shared to all the
objects.

The static variable can be used to refer to the common property of all objects (which is not unique
for each object), for example, the company name of employees, college name of students, etc.

Static variable gets a memory once at the time of class loading.

Example of static Variable:


package com.oops.basic;

public class StaticExample3 {

int studentId;
String studentName;
static String clg = "prmit&r";

public StaticExample3(int studentId, String studentName) {


this.studentId = studentId;
this.studentName = studentName;

void print() {

System.out.println(studentId + " " + studentName + " " + clg);


}

public static void main(String[] args) {


// TODO Auto-generated method stub
StaticExample3 st = new StaticExample3(111, "Rohan");
st.print();
StaticExample3 st1 = new StaticExample3(112, "Adesh");
st1.print();
StaticExample3 st2 = new StaticExample3(113, "Sahil");
st2.print();
StaticExample3 st3 = new StaticExample3(114, "Nishant");
st3.print();
}

Output:
111 Rohan prmit&r
112 Adesh prmit&r
113 Sahil prmit&r
114 Nishant prmit&r

2)Methods:

If you apply static keyword with any method, it is known as static method.static method belongs to
the class rather than the object of a class.It can be invoked without the need for creating an instance
of a class.A static method can access static data member and can change the value of it.

You might also like