-1

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.

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Qu0rk
  • 371
  • 1
  • 3
  • 9
  • 1
    Would downvoters be willing to explain their reasons for downvotes? I am not interested in arguing, but would like to be a better part of the SE community. – Qu0rk Apr 16 '14 at 18:49

3 Answers3

2

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

}
tutak
  • 1,120
  • 1
  • 15
  • 28
  • My mistake- I actually entered `Hello_World`, but I copy-pasted the `Hello()` in by mistake. Will edit to reflect this. – Qu0rk Apr 16 '14 at 18:40
  • @Qu0rk In that case please post your complete code. So we can pinpoint what is the mistake. – tutak Apr 16 '14 at 18:41
  • @Qu0rk Your code lacks main() method which is the entry point of the program. Try the code I proposed, let me know if you don't understand it. – tutak Apr 16 '14 at 18:50
  • @Qu0rk Your code had two issues 1- you didn't define the type of variable greet(by writing only greet instead of Hello_World greet) 2- Your class lacked main() method. – tutak Apr 16 '14 at 19:05
1

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

Qu0rk
  • 371
  • 1
  • 3
  • 9
xxx
  • 182
  • 7
  • Just to be clear, this answer was chosen for the sake of simplicity. Other answers were more complex, which is a severe handicap in the Hello World context. – Qu0rk Apr 16 '14 at 19:02
0

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

maxx777
  • 1,320
  • 1
  • 20
  • 37
  • dear down voter, the asker has edited the code later. so you must check it before down voting :( even the commentors on the question have said the same thing. – maxx777 Apr 16 '14 at 19:10
  • or atleast you should tell why it has been down voted so that further answers can be improved. – maxx777 Apr 16 '14 at 19:15