What is static in Java?
static is a keyword in Java that means:
“This belongs to the class, not to the object.
That means you can use it without creating an object.
Example 1: Static Variable
public class Student {
static String collegeName = "ABC College"; // shared by all students
}
Why use static?
Use Case Reason
Common data Same value for all objects
Utility methods Like Math.sqrt(), no object needed
Main method Java starts with static main()
What is non-static in Java?
non-static means the variable or method belongs to the object, not the class.
So to use it, you must create an object of the class.
Example: Non-Static Variable & Method
public class Student {
String name = "Sundar"; // non-static variable
void show() { // non-static method
System.out.println("Student Name: " + name);
}
public static void main(String[] args) {
Student s = new Student(); // create object
s.show(); // call non-static method
}
}
What is return in Java?
Send a value back from a method to the place it was called.
Or to exit from a method.
Example 1: Return an int value
public class Example {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(10, 20);
System.out.println("Sum: " + result); // Output: Sum: 30
}
}
What is an Object in Java?
In Java, an object is an instance of a class.
It is created to use variables and methods defined inside a class.
How to Create an Object?
ClassName objectName = new ClassName();
ClassName: the name of the class
objectName: the name you choose (like a variable)
new: creates memory (keyword)
ClassName() is the constructor (runs when object is created)
Example:
public class Student {
String name = "Sundar";
void display() {
System.out.println("Student name: " + name);
}
public static void main(String[] args) {
Student s = new Student(); // ✅ Object creation
s.display(); // ✅ Method call using object
}
}
What is a Method in Java?
A method is a block of code that performs a specific task.
You can call a method whenever you want to run that task.
Think of a method like a function — it helps you avoid repeating the same code
Why use Methods?
To organize code into reusable blocks
To avoid writing the same code again and again
To make code easier to read and maintain
Example:
public class Greet {
void sayHello() {
System.out.println("Hello, welcome!");
}
public static void main(String[] args) {
Greet g = new Greet(); // object creation
g.sayHello(); // method call
}
}
Top comments (0)