Variables
- Static variable
- Non-static variable
Static Variable
A static variable is a variable that belongs to the class, not to a specific object.
- Declared using the static keyword.
- Only one copy of the variable is created.
- It is shared by all objects of the class.
- It can be accessed using the class name.
public class Student {
static String college = "College";
public static void main(String[] args){
System.out.println(Student.college);
}
}
Non-Static Variable
A non-static variable belongs to a specific object of the class.
- It is declared without the static keyword.
- Each object gets its own copy.
- Its value can be different for each object.
- It is accessed using an object.
public class Student {
String name;
public static void main(String[] args){
Student s1 = new Student();
s1.name = "Ganapathi";
Student s2 = new Student();
s2.name = "Kumar";
System.out.println(s1.name);
System.out.println(s2.name);
}
}
Java OOP(Object Oriented Programming):
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects that contain data (fields) and behavior (methods). It focuses on designing software that closely represents real-world entities.
Class
- A Class in Java is a blueprint or template used to create objects.
- It defines the properties (data) and behaviors (methods) that objects of that class will have.
Java Class Naming Rules
- Meaningful name
- Start's with uppercase
- No space allowed
- No special character allowed[$,_]
- Cannot start with a number
public class SuperMarket
{
//-----------
}
Object
- An Object in Java is an instance of a class that represents a real-world entity.
It is used to access the variables and methods defined inside a class.
State: Represents the current data or attributes of an object.
Behavior: Represents the actions that an object can perform.
public class Student {
String name;
public static void main(String[] args){
Student s1 = new Student();
s1.name = "Ganapathi";
Student s2 = new Student();
s2.name = "Kumar";
System.out.println(s1.name);
System.out.println(s2.name);
}
}

Top comments (0)