Image referred from: http://www.sitesbay.com/java/images/basic-java/Static-and%20non-Static-Method-rules.png
static:
In Java, the ‘static’ keyword is used to declare members (variables and methods) that belong to the class itself, rather than to individual instances of the class.
When a member is declared as ‘static’, it means that it is shared among all instances of the class. This allows you to access the member without creating an object of the class.
Static Variables: A static variable is shared by all instances of a class. It is also known as a class variable. To declare a static variable, you use the ‘static’ keyword before the variable declaration.
public class MyClass{ static int myNumber; //static variable }
Static variables exist inside a class and outside a method. To access a static variable, you use the class name followed by the variable name: MyClass.myNumber
.
Static Methods: A static method belongs to the class itself, and not to any particular instance of the class. You can call a static method without creating an object of the class. To declare a static method, you use the static keyword before the method declaration.
public class MyClass{ public static void myMethod(){ //static method } }
To call a static method, you use the class name followed by the method name: MyClass.myMethod()
.
Static Blocks: A static block is a block of code that is executed only once when the class is loaded into memory. It is useful for initializing static variables or performing any other one-time setup. Static blocks are enclosed in curly braces and preceded by the static keyword.
public class MyClass{ static{ //static block } }
The static block will be executed before any static variables or static methods are accessed.
non-static:
In Java, the non-static (instance/ object) members are variables and methods that are associated with individual instances (objects) of a class. They are declared without the static keyword and are unique to each object created from the class.
Non-static Variables: Non-static variables, also known as instance variables, hold unique values for each object of a class. They are declared without the static keyword. Each object created from the class has its own copy of these variables.
public class MyClass{ int myNumber; //non-static variable }
To access non-static variables, you need to create an object of the class and then use the object reference followed by the variable name.
MyClass obj = new MyClass(); obj.myNumber;
.
Non-Static Methods: Non-static methods are associated with individual objects of a class and can access non-static variables and other non-static methods. They are declared without the
static keyword.
public class MyClass{ public void myMethod(){ //non-static method } }
To call a non-static method, you need to create an object of the class and then use the object reference followed by the method name: MyClass obj = new MyClass(); obj.myMethod()
.
Non-Static Blocks: Non-static blocks are used to initialize non-static variables or perform additional initialization logic for each object. They are executed every time an object of the class is created. Non-static blocks are enclosed in curly braces and do not have any specific keyword.
public class MyClass{ { //non-static block } }
The non-static block will be executed every time an object of the class is created before the constructor is called.
Courtesy: chat.openai.com