Solved

Inner Class in Java

Posted on 2011-03-21
14
260 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
Comment Utility
>>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
Comment Utility
Oops - ignore that sorry
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
>>"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
Comment Utility
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
Comment Utility
It's not declared as static

static void drive() ............
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
Comment Utility
> 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
Comment Utility
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
Comment Utility
d.drive()

is not the same as

Car.drive()
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
Thank you for your help!
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
I was only focusing on giving points to the participants that I was not aware of the comments. Sorry about that!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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