Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Inner Class in Java

Posted on 2011-03-21
14
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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