Java Basics

Access Modifiers in Java

Java provides four types of access modifiers to set the accessibility of classes, methods, and variables:

  • Public: Accessible from anywhere in the program.
  • Private: Accessible only within the declared class.
  • Protected: Accessible within the same package and by subclasses.
  • Default: (No modifier) Accessible only within the same package.

public class Example {
    public int publicVar;
    private int privateVar;
    protected int protectedVar;
    int defaultVar; // default access
}
    

Package in Java

Packages are used to group related classes and interfaces. They help to avoid name conflicts and to control access. To create a package, use the package keyword at the top of your Java file.


package com.example.myapp;

public class MyClass {
    // class code
}
    

Java Automatic Numeric Type Promotion

Java automatically promotes smaller numeric types to larger ones when performing operations. For example, when you perform an operation involving an int and a double, the int is promoted to double.


int a = 5;
double b = 6.2;
double result = a + b; // 'a' is promoted to double
    

Why Class Name And File Name Should be Same in Java

In Java, the class name and file name should be the same to ensure that the Java compiler can locate the class. This is especially important for public classes.


// File name: MyClass.java
public class MyClass {
    // class code
}
    

Why main Method static in Java

The main method is static because it needs to be accessible before any objects are created. This allows the JVM to call the main method without creating an instance of the class.


public class MainClass {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
    

Java Pass by Value or Pass by Reference

Java is strictly pass-by-value. When you pass an object, you are passing the reference to it by value, meaning the reference itself is copied.


public class Example {
    public static void main(String[] args) {
        int num = 10;
        modifyValue(num);
        System.out.println(num); // Output: 10
    }

    static void modifyValue(int value) {
        value = 20;
    }
}
    

Java - Could not find or load main class error Fix

This error usually occurs due to incorrect classpath settings or a mismatch between the class name and file name. Ensure that the class name matches the file name and that the classpath is set correctly.

this Keyword in Java With Examples

The this keyword refers to the current instance of the class. It is used to access class variables and methods.


public class Example {
    int x;

    Example(int x) {
        this.x = x; // 'this' refers to the current instance
    }
}
    

super Keyword in Java With Examples

The super keyword is used to refer to the immediate parent class object. It is used to call parent class methods and constructors.


class Parent {
    void display() {
        System.out.println("Parent class method");
    }
}

class Child extends Parent {
    void display() {
        super.display(); // calls Parent's display method
        System.out.println("Child class method");
    }
}
    

final Keyword in Java With Examples

The final keyword is used to declare constants, prevent method overriding, and prevent inheritance.


final class FinalClass {
    final int x = 10;

    final void display() {
        System.out.println("This is a final method.");
    }
}
    

strictfp in Java

The strictfp keyword is used to restrict floating-point calculations to ensure portability across different platforms.


strictfp class Example {
    strictfp void calculate() {
        // method code
    }
}
    

finalize() Method in Java

The finalize() method is called by the garbage collector before an object is destroyed. It is used to perform cleanup operations.


public class Example {
    protected void finalize() {
        System.out.println("Object is being garbage collected");
    }
}
    

Type Casting in Java With Conversion Examples

Type casting is used to convert one data type into another. Java supports both implicit and explicit type casting.


int i = 100;
long l = i; // Automatic type promotion (implicit casting)
int j = (int) l; // Explicit type casting
    

Why no Multiple Inheritance in Java

Java does not support multiple inheritance to avoid complexity and simplify the design. Instead, Java uses interfaces to achieve multiple inheritance.


interface A {
    void methodA();
}

interface B {
    void methodB();
}

class C implements A, B {
    public void methodA() {
        System.out.println("Method A");
    }

    public void methodB() {
        System.out.println("Method B");
    }
}