File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphic Method Call

 
Mike Cutter
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am taking a Java class and we are covering polymorphism.

I have to utilize the given code in my assignment:


I put all this into one big public class. When I complied it, I get the following error:

<identifier> expected
x.a2();

How do I get this polymorphic method call x.a2(); to compile without errors?

Thanks,

Mike

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the exact code that you are trying to compile look like?

Note that you cannot put statements like x.a2(); at class level - statements have to be inside a method. So, something like this is not going to work:

You need to put the statement inside a method, for example the main() method:

 
Mike Cutter
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put the x.a2(); in the main() method.

I now get this error:

non-static variable this cannot be referenced from a static context
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x is a non-static field. To access it from any static method you need an instance of C. So either create a new C and use it's x field, or make x static.
 
Mike Cutter
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code:



I get the following compilation errors:

non-static variable x cannot be referenced from a static context
x.a2();
non-static variable z cannot be referenced from a static context
z.a2();
non-static variable z cannot be referenced from a static context
z.a1();
non-static variable x cannot be referenced from a static context
x.a1();

I'll admit I am pretty lost on why the syntax is wrong and the logic is not making sense.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x, y and z are all declared inside the class and are therefore non-static instance fields. Notice the word "instance" - you need an instance of class MyClass to access them.

There are three ways to solve this:
1) create an instance of MyClass (let's call it "my"), then use that to access them ("my.x.a2()").
2) make them static; simply put the static keyword in front of their declarations.
3) move them inside the main method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic