DEV Community

ViGnEsH
ViGnEsH

Posted on

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 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);
}
}
Enter fullscreen mode Exit fullscreen mode

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);

}
}
`

Top comments (0)