Thursday, 11 April 2024

Static vs Non-static access

 class Car{

    void get()
    {
        System.out.println("Inside Car");
        MyClass.func(); // non-static can access static
    }
}
public class MyClass {

    public static void main(String[] args) {
        func();
        Car cr = new Car();
        cr.get();
    }
    public static void func()
    {
        System.out.println("Hello");
        //Car.get(); // static cannot access non-static
    }
}

No comments:

Post a Comment

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...