Static Classes

// Hello World Using static class
public class HelloStatic {
    // Java standard runtime entry point
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

HelloStatic.main(null);
Hello World!

Methods

Setters and Getters

Setters

Getters

// Addition class
public class Addition {
    // var for result
    private int result;
    
    // static version
    public Addition() {
        this.setAdd(3, 5); 
    }

    // argument constructor version
    public Addition(int A, int B) { 
        this.setAdd(A, B);
    }

    // setter
    public void setAdd(int A, int B) {
        this.result = A + B;
    }

    public int getAdd() {
        return this.result;
    }

    public static void main(String[] args) {
        Addition hd1 = new Addition(); // no-argument constructor
        Addition hd2 = new Addition(10, 5); // constructor with arguments
        System.out.println(hd1.getAdd());
        System.out.println(hd2.getAdd());
    }
}


Addition.main(null);
8
15

Example Test Method using Scanner

import java.util.Scanner;

public class Test {
    // Question Method
    public void Question1() {
        String A1 = "Y"; // Corrected variable declaration
        System.out.println("Is the sky blue? Y or N");

        // Initialize Scanner Object
        Scanner myObj = new Scanner(System.in);

        // Setting a variable using Scanner
        String response = myObj.nextLine();

        // Convert the response to lowercase for comparison
        response = response.toLowerCase();

        // Logic for the Question
        if (response.equals("y")) { // Use .equals() to compare strings
            System.out.println("Correct");
        } else if (response.equals("n")) { // Use else if for the second condition
            System.out.println("False");
        } else {
            System.out.println("Wrong Input");
        }
    }

    public static void main(String[] args) {
        // Create an instance of the Test class
        Test test = new Test();
        // Call the Question1 method on the instance
        test.Question1();
    }
}


Test.main(null);
Is the sky blue? Y or N
Correct

Hacks