This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Polymorphic Method Call  RSS feed

 
Ranch Hand
Posts: 49
  • 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

 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • 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
  • 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
 
Sheriff
Posts: 21421
94
Chrome Eclipse IDE Java Windows
  • 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
  • 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
Posts: 21421
94
Chrome Eclipse IDE Java Windows
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!