Dev.to WebDev 🛠 Dev 👁 1

Static vs Non-Static in Java

July 3 2026 Static Static refers to class-specific information; only one memory copy is needed. Memory is allocated only once. It can be accessed without creating an object. Shared by all objects of the cla

July 3 2026

Static

Static refers to class-specific information; only one memory copy is needed.

  • Memory is allocated only once.
  • It can be accessed without creating an object.
  • Shared by all objects of the class.

Example

class Fruits
{

static String fruitName ="Apple";
static String fruitName1 ="Mango";

public static void main(String[] args){

System.out.println("1 "+Fruits.fruitName+" $" +Fruits.price);
}
}

Non-Static

Non-static used has object-specific information
(or)
Object is a memony refers of class`

  • Each object has its own copy.
  • You must create an object to access it.
  • Different objects can store different values.

`java
class Fruits
{
String fruitName2 = "Banana";
int price2 = 10;

public static void main(String[] args){

Fruits basket = new Fruits();
System.out.println("3 "+basket.fruitName2+" $ "+basket.price2);

}
}
`

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.