Solved

Inner Class in Java

Posted on 2011-03-21
14
262 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Jpanels and Jframe 8 32
egit plugin on eclipse 8 62
How to convert String matching to regex in java 4 30
servlet example 11 40
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

816 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