Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inner Class in Java

Posted on 2011-03-21
14
Medium Priority
?
271 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

877 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