Solved

Inner Class in Java

Posted on 2011-03-21
14
261 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:techbro
  • 5
  • 5
  • 4
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35184793
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35184798
Oops - ignore that sorry
0
 
LVL 92

Expert Comment

by:objects
ID: 35184802
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
 
LVL 92

Expert Comment

by:objects
ID: 35184810
Car.this.drive() on the other hand says to call the drive() method in the outer class named Car, which does exist
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 35184814
>>"Car.drive()"

drive is not a static method of Car

>>"this.drive()"

drive is not a method of 'this' (Engine)
0
 

Author Comment

by:techbro
ID: 35184905
Appreciate your response.
Could you please explain what you mean by "drive() is not a static method of class Car"?

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35184919
It's not declared as static

static void drive() ............
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 35184959
> 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
 

Author Comment

by:techbro
ID: 35184971
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
 
LVL 92

Expert Comment

by:objects
ID: 35185008
d.drive()

is not the same as

Car.drive()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35185050
>>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
 

Author Closing Comment

by:techbro
ID: 35185060
Thank you for your help!
0
 
LVL 92

Expert Comment

by:objects
ID: 35185073
The accepted comment just copies what I had already posted at 35184802
why did you accept it and not mine?
0
 

Author Comment

by:techbro
ID: 35185131
I was only focusing on giving points to the participants that I was not aware of the comments. Sorry about that!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now