开发者

Very confused with calling method using class name in following java code

开发者 https://www.devze.com 2023-03-24 06:34 出处:网络
class printer { void print(String p) { System.o开发者_高级运维ut.println(p); } } class test { public static void main(String args[])
class printer
{
   void print(String p)
   {
      System.o开发者_高级运维ut.println(p);
   }
}

class test
{
   public static void main(String args[])
   {
      (new printer()).print("Hello World!");
   }
}

The above code is 100% right but

Can anyone please explain me that

How the code at line no.13 is true.

As i have heard that the members that are static can only be called using its class name

Thus how come the above code turned to be true even when the method "print" is not static.


When discussing method access rules,

 (new Printer()).print("Hello World!");

is the same as

Printer printer = new Printer();
printer.print("Hello World!");

However,

Printer.print("Hello World!");

would be static access and not allowed.

(new Printer())

makes a new instance but does not give it a reference.


You made yourself a new printer object and then invoked its method.

public static void main()

is the standard way into any java application, you are running a static methoid of class test.

Inside that you create an object, easier to see if you code like this:

printer myPrinter = new printer();

now you have an object you can invoke its methods, print() is not static

myPrinter.print("I'm not static");


(new printer()) refers to an instnce of Printer and you are calling non static method on that instance that is perfectly fine.


"As i have heard that the members that are static can only be called using its class name"

No, static member can be called either directly from the class name or any instances of that class.

"Thus how come the above code turned to be true even when the method "print" is not static"

because instance method can be called from anywhere as far as it is visible from the invoker, in this case, (new printer()) create a new instance of the printer class, and print() is a method from that instance,
thus it can be invoked from static method main()


I think you're getting static classes confused with static methods. If the class was static you wouldn't have been able to instantiate it with the 'new' keyword. By invoking the 'new' keyword on line 13 you're creating a new object of type 'printer' and by wrapping it in the parentheses you're just using shorthand for saying:

printer myPrinter = new printer();
myPrinter.print("Test");

without the reuse capabilities of naming the printer object.


in line : (new printer()).print("Hello World!"); You are creating a new object of printer and calling an instance method of that class. That has nothing to do with static keyword.

public static void main(String args[]) : is defined by java which is a way to start any java application.

And static methods are shared by all the objects, whether you call from Class or with any instance.


If the print method is not static, you can instantiate an object and call the print method on that object (as you have done). If the print method was static instead i.e.

   static void print(String p)
   {
      System.out.println(p);
   }

Then you would have to use the class name to call the print method i.e. printer.print("hi"). You should know that static methods are class methods i.e. all objects of the class use the same method.

With static print, note that even if you did create an object and call print on the object (as you did), this would still work but your IDE would typically show a warning, advising you to use the class name instead of the instance.

Now suppose you defined the printer class inside Test i.e. nested classes

class Test
{
   public static void main(String args[])
   {
      new Test().new printer().print("Hello World!");
   }
   class printer
   {
      void print(String p)
      {
         System.out.println(p);
      }
   }
}

Then you can see that to call the print method, you would first have to instantiate a Test class object (outer class), then instantiate a printer object using the Test object (inner class) and then call print on the printer object. Why? This is because the printer class is not static, hence, you need to create an instance of it. Alternatively, if you declare your print method static i.e.

class Test
{
   public static void main(String args[])
   {
       printer.print("Hello World!");
   }
   static class printer//note: only nested classes can be static
   {
      static void print(String p)
      {
         System.out.println(p);
      }
   }
}

Then you can see that you can call print using the class name printer. The moral of the story is that each time you see the word static, expect all objects to share that method (or variable) and hence, when you call that method (or variable), be sure to use the class name directly.

Also, it is good to follow naming conventions- in other words, use the name Printer rather than printer for the class.

0

精彩评论

暂无评论...
验证码 换一张
取 消