This tutorial covers key Java interview questions tailored for freshers. These questions are designed to help you confidently tackle the technical rounds of your Java interviews, making it easier to land your dream job as a Java developer.
Join Our WhatsApp Community | CLICK HERE |
Join Our Instagram Community | CLICK HERE |
Table of Contents
1) Discuss public static void main(String args[])
- public: This is an access modifier. It means the method can be accessed by any class.
- static: This allows the method to be called without creating an object of the class. Since the program starts with the main method, it must be static.
- void: This is the return type of the method, meaning it does not return any value.
- main: This is the name of the method that the Java Virtual Machine (JVM) looks for to start program execution.
- String args[]: This is used to store command-line arguments passed when the program runs.
2) Define class and object. Explain them with an example using Java.
- Class: A class is like a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects of the class will have.
- Object: An object is an instance of a class. It represents a real-world entity with attributes and behaviors.
Example:
// Defining a class
class Car {
String brand; // Attribute
int speed; // Attribute
// Method to display details
void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
}
// Creating and using objects
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object
myCar.brand = "Tesla"; // Setting attributes
myCar.speed = 200;
myCar.displayDetails(); // Calling the method
}
}
Output:
Brand: Tesla
Speed: 200 km/h
In this example, Car
is the class, and myCar
is an object of the Car
class. The object uses the attributes and methods defined in the class.
3) What are constructors in Java?
A constructor is a special method in Java used to initialize an object. It has the same name as the class and does not have a return type. A constructor is automatically called when an object is created.
If no constructor is explicitly defined in a class, the Java compiler provides a default constructor that has no arguments.
Types of Constructors:
- Default Constructor: A constructor with no parameters.
- Example:
class Example { int number; // Default constructor Example() { number = 0; } }
- Example:
- Parameterized Constructor: A constructor with parameters, allowing you to pass values while creating objects.
- Example:
class Example { int number; // Parameterized constructor Example(int num) { number = num; } }
- Example:
Example Usage:
class Car {
String brand;
int speed;
// Default constructor
Car() {
brand = "Unknown";
speed = 0;
}
// Parameterized constructor
Car(String b, int s) {
brand = b;
speed = s;
}
void display() {
System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
Car defaultCar = new Car(); // Calls default constructor
Car customCar = new Car("Tesla", 200); // Calls parameterized constructor
defaultCar.display();
customCar.display();
}
}
Output:
Brand: Unknown
Speed: 0 km/h
Brand: Tesla
Speed: 200 km/h
Constructors simplify object initialization and enhance code readability.
4) What are constructors in Java?
A constructor is a special method in Java used to initialize an object. It has the same name as the class and does not have a return type. A constructor is automatically called when an object is created.
If no constructor is explicitly defined in a class, the Java compiler provides a default constructor that has no arguments.
Types of Constructors:
- Default Constructor: A constructor with no parameters.
- Example:
class Example { int number; // Default constructor Example() { number = 0; } }
- Example:
- Parameterized Constructor: A constructor with parameters, allowing you to pass values while creating objects.
- Example:
class Example { int number; // Parameterized constructor Example(int num) { number = num; } }
- Example:
Example Usage:
class Car {
String brand;
int speed;
// Default constructor
Car() {
brand = "Unknown";
speed = 0;
}
// Parameterized constructor
Car(String b, int s) {
brand = b;
speed = s;
}
void display() {
System.out.println("Brand: " + brand);
System.out.println("Speed: " + speed + " km/h");
}
}
public class Main {
public static void main(String[] args) {
Car defaultCar = new Car(); // Calls default constructor
Car customCar = new Car("Tesla", 200); // Calls parameterized constructor
defaultCar.display();
customCar.display();
}
}
Output:
Brand: Unknown
Speed: 0 km/h
Brand: Tesla
Speed: 200 km/h
Constructors simplify object initialization and enhance code readability.
5) What are the different ways to create objects in Java?
In Java, objects can be created in the following ways:
1. Using new
Keyword
The most common and straightforward way to create objects.
class Example {
int num = 10;
}
public class Main {
public static void main(String[] args) {
Example obj = new Example(); // Object creation using new
System.out.println(obj.num);
}
}
2. Using Class.newInstance()
Method
This method creates an object by invoking the no-argument constructor. It is part of Java’s reflection.
class Example {
int num = 20;
}
public class Main {
public static void main(String[] args) throws Exception {
Example obj = Example.class.newInstance(); // Object creation using reflection
System.out.println(obj.num);
}
}
3. Using clone()
Method
The clone()
method creates a copy of an existing object. The class must implement the Cloneable
interface.
class Example implements Cloneable {
int num = 30;
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Example obj1 = new Example();
Example obj2 = (Example) obj1.clone(); // Object creation using clone
System.out.println(obj2.num);
}
}
4. Using Deserialization
An object is created when a serialized object is read back into memory.
import java.io.*;
class Example implements Serializable {
int num = 40;
}
public class Main {
public static void main(String[] args) throws Exception {
Example obj = new Example();
// Serialize
FileOutputStream fos = new FileOutputStream("file.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.close();
// Deserialize
FileInputStream fis = new FileInputStream("file.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Example obj2 = (Example) ois.readObject();
ois.close();
System.out.println(obj2.num); // Object creation using deserialization
}
}
5. Using Constructor.newInstance()
Method
This method is part of Java’s reflection mechanism and is used to create objects.
import java.lang.reflect.Constructor;
class Example {
int num = 50;
}
public class Main {
public static void main(String[] args) throws Exception {
Constructor<Example> constructor = Example.class.getConstructor();
Example obj = constructor.newInstance(); // Object creation using reflection
System.out.println(obj.num);
}
}
These approaches provide flexibility in object creation based on specific requirements.
6) Why can static methods not access non-static variables or methods?
Static methods belong to the class rather than any specific object of the class. They can be called without creating an instance of the class.
Non-static variables or methods, on the other hand, are tied to specific instances of the class. Since static methods do not operate on any instance, they cannot access instance variables or methods directly, as those require an object to exist and be initialized.
For example:
class Example {
int num = 10; // Non-static variable
static void staticMethod() {
// System.out.println(num); // Error: Cannot access non-static variable
System.out.println("Static methods cannot access non-static members directly.");
}
}
public class Main {
public static void main(String[] args) {
Example.staticMethod();
}
}
To access non-static members, you must create an instance of the class within the static method.
class Example {
int num = 10; // Non-static variable
static void staticMethod() {
Example obj = new Example();
System.out.println(obj.num); // Access via object
}
}
public class Main {
public static void main(String[] args) {
Example.staticMethod();
}
}
This limitation ensures that static methods remain independent of any specific instance.
7) Why can static methods not access non-static variables or methods?
Static methods belong to the class rather than any specific object of the class. They can be called without creating an instance of the class.
Non-static variables or methods, on the other hand, are tied to specific instances of the class. Since static methods do not operate on any instance, they cannot access instance variables or methods directly, as those require an object to exist and be initialized.
For example:
class Example {
int num = 10; // Non-static variable
static void staticMethod() {
// System.out.println(num); // Error: Cannot access non-static variable
System.out.println("Static methods cannot access non-static members directly.");
}
}
public class Main {
public static void main(String[] args) {
Example.staticMethod();
}
}
To access non-static members, you must create an instance of the class within the static method.
class Example {
int num = 10; // Non-static variable
static void staticMethod() {
Example obj = new Example();
System.out.println(obj.num); // Access via object
}
}
public class Main {
public static void main(String[] args) {
Example.staticMethod();
}
}
This limitation ensures that static methods remain independent of any specific instance.
8) What is a static class?
In Java, there is no concept of a fully static class like in some other languages (e.g., C#). However, you can mimic the behavior of a static class by:
- Declaring all variables and methods as
static
. - Making the constructor private to prevent object instantiation.
This ensures that the class cannot be instantiated and is only accessible through the class name.
Example:
class StaticClassExample {
// Static variables and methods
static int num = 10;
static void display() {
System.out.println("Static method called. Num = " + num);
}
// Private constructor to prevent instantiation
private StaticClassExample() {
// Prevent object creation
}
}
public class Main {
public static void main(String[] args) {
// Accessing static class members directly
StaticClassExample.display();
System.out.println("Accessing static variable: " + StaticClassExample.num);
}
}
Key Points:
- Usage: A static class is helpful for utility or helper classes (e.g.,
Math
class in Java). - Limitation: It cannot be instantiated because of the private constructor.
This design ensures the class is used only through its static members and behaves similarly to a static class in other languages.
2 thoughts on “Master Java Interview Questions and Answers for Freshers 2025”