• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Polymorphic Method Call

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic