I have just started out with Java. I have been programing with C and C++ for some years now.

I have this problem. Im not able to call a non static method belonging to the main class from the static main method.

This is the code:

public class Helloworld {

	private  void call()
	{
		System.out.println(" World");
	}
	public static void main(String[] args) {
		System.out.println("Hello"); 
		call();
	}
	
}

Thanks!

Because you cannot call non-static methods from a static context without using an instance of the class containing those methods. i.e.

Whatever variable = new Whatever();
variable.someMethod();

So I should create an instance of the Helloworld class and use it to call its method from main???

EDIT:
Thanks worked!

public class Helloworld {

	private  void call()
	{
		System.out.println(" World");
	}

	public static void main(String[] args) {
		System.out.println("Hello"); 
                Helloworld hello=new Helloworld();
		hello.call();
	}
	
}

BTW, if I instantiate the object outside the main method, it doesnt work. why?

Edited 5 Years Ago by stevanity: n/a

public class Helloworld {

        Helloworld hello=new Helloworld(); 
	private  void call()
	{
		System.out.println(" World");
	}

	public static void main(String[] args) {
		System.out.println("Hello"); 
                
		hello.call();
	}
	
}

This way.

BTW, what is a difference between class variable and instance.

A class variable has one value which is associated with the class itself.
An instance variable has zero or more values, one for each instance of the class that has been created. You can only use it by specifying which instance's value you are referring to, as in hello.call() or, within an instance method, just call(), which is short for this.call(); (ie use the value for the current instance)
In c/c++ terms, a static variable's memory is part of the class's memory, allocated when the class definition is loaded and is never released. An instance variable's memory is allocated dynamically each time an instance of the class is created, and is released when the instance is garbage collected.
Variables are instance variables unless you declare them static.

So in your latest code hello is an instance variable. In your main method (a class method) there is no instance of Helloworld that can be used to resolve hello's value, so the reference is an error.
If you change your declaration of hello to be static then your code will work - hello will only have one value (associated with the class itself) and you can correctly use it within a static method.

in your first code, you could also make the call method static.
this way, you could call it from within your main method without having to instantiate an object

A class variable has one value which is associated with the class itself.
An instance variable has zero or more values, one for each instance of the class that has been created. You can only use it by specifying which instance's value you are referring to, as in hello.call() or, within an instance method, just call(), which is short for this.call(); (ie use the value for the current instance)
In c/c++ terms, a static variable's memory is part of the class's memory, allocated when the class definition is loaded and is never released. An instance variable's memory is allocated dynamically each time an instance of the class is created, and is released when the instance is garbage collected.
Variables are instance variables unless you declare them static.

So in your latest code hello is an instance variable. In your main method (a class method) there is no instance of Helloworld that can be used to resolve hello's value, so the reference is an error.
If you change your declaration of hello to be static then your code will work - hello will only have one value (associated with the class itself) and you can correctly use it within a static method.

SO what you are saying is, if i create the instance outside main method, then the call inside the main method cannot resolve the instance? So I should instantiate the object hello within the static method (main) itself?

Almost.
If you create the instance outside main method as an initialiser for a static variable the you can use that variable within main to access the instance and all the instance's fields.
Your code creates the instance outside main method as an initialiser for a instance variable. That variable will be initialised (and thus create an instance) once for each time an instance of the class is created. If you don't create an instance, then the initialiser will never be executed. You can create that instance inside your main if you want, and that will give you a reference to that instance that you can use... except...
There's an additional complexity in the way the code is constructed:
Helloworld hello=new Helloworld(); will be executed once every time an instance of Helloworld is created. Each time it's executed it creates a new instance, so you have a recursive loop that results very quickly in a stack overflow!

Edited 5 Years Ago by JamesCherrill: n/a

I see. Thank you very much. Lots to learn in Java.

My prior knowledge in C# helps a lot. Looks like C# is just a rip from Java.

Looks like C# is just a rip from Java.

Now you're catching on. They had to do something with the invalid JVM that they developed.

This question has already been answered. Start a new discussion instead.