Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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();
 }

}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top