简体   繁体   中英

Error in “Hello World” when compiling on Dr. Java?

I am learning Java, and am testing a simple 'Hello World' program given to me by my teacher. I am using Dr. Java on 64-bit Ubuntu 12.04 LTS.

Code is below:

public class Hello_World
{
    public void go()
    {
        System.out.println("Hello, World!");
    }
}

I hit the F5 key, and the code compiles. After that, I enter the lines below:

greet = new Hello_World();
greet.go();

The output is supposed to be Hello, World! , but I am getting Static Error: Undefined name 'greet' instead. What am I doing wrong?

Please forgive me if I this is an easy fix (it probably is). I searched SE, but did not find anything that helped.

it should be:

Hello_World greet = new Hello_World();
greet.go();

The class you defined is called Hello_Word not Hello.

EDIT

Your complete code should look something like:

public class Hello_World
{
    public void go()
    {
        System.out.println("Hello, World!");
    }


public static void main(String[] args){
   Hello_World greet = new Hello_World();
   greet.go();
 }

}

You need the code:

Hello_World greet = new Hello_World();
greet.go();

in a main method, which is the point of execution of a java program. http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

Add the following code to your class

public static void main(String args[]){
  Hello_World greet=new Hello_World();
  greet.go();
}

Since you are running application on your console, you need to have a main() method

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