Inner Class in Java

I want the output "hi" by print statement.
In line 6, if I use "drive()" or "Car.this.drive()", I get the desired output.
I like to know why it causes compiler error if I use "Car.drive()" or "this.drive()" in line 6 to print the output.

public class Car
{
	class Engine
	{
		{
			drive();
		}
	}
	
	public static void main(String [] args)
	{
		new Car().go();
	}
	
	void go()
	{
		new Engine();
	}
	
	void drive()
	{
		System.out.println("hi");
	}
}

Open in new window

techbroAsked:
Who is Participating?
 
CEHJCommented:
>>"Car.drive()"

drive is not a static method of Car

>>"this.drive()"

drive is not a method of 'this' (Engine)
0
 
CEHJCommented:
>>I like to know why it causes compiler error if I use "Car.drive()" or "this.drive()" in line 6 to print the output.

Because there's no enclosing instance of Car

0
 
CEHJCommented:
Oops - ignore that sorry
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
objectsCommented:
Car.drive() is to call a static method in Car class. there is not one
this.drive() is to call a drive method in the Engine class which again does not exist
0
 
objectsCommented:
Car.this.drive() on the other hand says to call the drive() method in the outer class named Car, which does exist
0
 
techbroAuthor Commented:
Appreciate your response.
Could you please explain what you mean by "drive() is not a static method of class Car"?

0
 
CEHJCommented:
It's not declared as static

static void drive() ............
0
 
objectsCommented:
> Could you please explain what you mean by "drive() is not a static method of class Car"?

static method do not require an instance of the class, and can be called using Classname.methodName()

http://leepoint.net/notes-java/flow/methods/50static-methods.html
0
 
techbroAuthor Commented:
When I am instantiating the class Car, it seems to work fine even if the "drive" method is not static.
I am sorry for the confusion, but why the "drive" needs to be static if I use Car.drive()?

class Engine
{
	{
		Car d = new Car();
		d.drive();
	}
}

Open in new window

0
 
objectsCommented:
d.drive()

is not the same as

Car.drive()
0
 
CEHJCommented:
>>but why the "drive" needs to be static if I use Car.drive()?

Because that's the form for calling a static method - which is also called a 'class method' (which should help you to understand whay)
0
 
techbroAuthor Commented:
Thank you for your help!
0
 
objectsCommented:
The accepted comment just copies what I had already posted at 35184802
why did you accept it and not mine?
0
 
techbroAuthor Commented:
I was only focusing on giving points to the participants that I was not aware of the comments. Sorry about that!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.