The difference between “public” and “public static”?

后端 未结 5 893
小鲜肉
小鲜肉 2021-01-30 06:58

What does static mean?

I know public means that it can be accessed from outside the class, and private only from inside the class…

5条回答
  •  有刺的猬
    2021-01-30 07:34

    Example:

    public class Methods_Test1 
    {   
        public static void Display(String Name)
        {
            System.out.println("Hello There " + Name);
            System.out.println("I am from Display method");
        }
    
    
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter name");
            String name = sc.next();
            Obj.Display(name);
    
        }
    

    The public static void Display(String name) method accessible as static method within its own class which can be accessed without creating object of the class where as the same method behaves as public for the outside classes which can be accessed by creating object.

提交回复
热议问题