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!

Recommended Answers

All 13 Replies

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?

Show how you instantiated it. As an instance or a class variable?

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

But you don't want to do that. There's a reason it's called "object-oriented" programming, yeah?

you should see the rule of the two methods -> Instance/Static methods

I fail at comprehension. Please make simple for my little brain.

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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.