简体   繁体   中英

why does this java code not work if the method name is different from the class?

package tut;
import java.awt.Graphics;
import javax.swing.JFrame;


public class javaconcepts extends JFrame
   {
      public void paint(Graphics g)
      {
              g.drawOval(100,50,50,50);
      }

public javaconcepts()
   {
    setSize(600,400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

   }
public static void main(String[] args)
   {
    javaconcepts guiWindow = new javaconcepts();
    guiWindow.setVisible(true);
   }    
}

Sorry for the very newbie question but I saw code in a textbook which I do not understand. The text book version created a smiley face but I just replicated the program so that it would display a simple circle instead. I would very much appreciate your help.

If I change the name of "public javaconcepts()" to anything different, it gives me an error which says to make it type void. Changing it to type void causes the program to not execute what is in the brackets of "public javaconcepts()".

I am self teaching so I don't have a teacher to ask. Thank you.

public javaconcepts() is a special "method"(see the comment) - a Constructor.

So why it doesn't work after a rename? In java, constructors dont return anything, and their name is the same as the enclosing class's name - so if you change one of them, the code breaks. If you chance class name - constructor is broken, because it is not a valid usual method - it has no return type. Have a look at this schema of method:

access-modifiers return-type method-name(parameters)

What a constructor is?

Constructor is a method which is called with the keyword new , and it creates and initializes new objects.

It's used at this line: javaconcepts guiWindow = new javaconcepts();

Constructor definition from: http://www.homeandlearn.co.uk/java/class_constructor.html

A method you can use to set initial values for field variables. When the object is created, Java calls the constructor first. Any code you have in your constructor will then get executed. You don't need to make any special calls to a constructor method - they happen automatically when you create a new object.

[by the new keyword]

public javaconcepts()

is a constructor and not a method. In Java constructors need to have the same name as class and they do not have return types.

For further reading: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Because it is a constructor. JLS-8.8. Constructor Declarations says (in part),

The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration; otherwise a compile-time error occurs.

In all other respects, the constructor declaration looks just like a method declaration that has no result ( §8.4.5 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM