• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Why am I getting this error?

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have created those two classes:

1-

package food;

public abstract class Fruit {

private int x;
private int y;
public abstract int getX();
}

2-

import food.Fruit;

class Apple {

public static void main(String[] args)
{

int x= 3;

public int getX()
{
return x;
}


}
}


Now, when I try to run "Apple.java", I get an error at the following line:

public int getX()

in class number (2).

Why is that?

Thanks.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's probably because you are trying to define the getX() method inside the main() method.

John.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a syntax error because you define "public int getX()..." inside the main method.

You also have a logical error because there is no instance field "x" in class Apple. In the Apple static method "main" you reference a local variable "x", but since it's a local variable, it isn't seen outside of the block (method) where it's defined. That variable "x" has nothing to do with apples or fruit.

You probably meant for class Apple does to extend class Fruit. Even that wouldn't work in this case, because you defined "x" in Fruit as a private field. That's appropriate, you don't *want* to be able to reference it in the sub-class. The proper way to implement your classes would be to define "x" and getX() in Fruit, as you've done, and implement getX() in Fruit rather than making it abstract. If you think getX() needs to be abstract, then get rid of the private x.

The problem with made-up examples like this is that they don't have meaning. What is "x"? How does it relate to fruit or apples? The whole point of OO is to model objects -- things like apples -- so it's hard to say what's the "right" way to implement a property called "x".
 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your replies.

I changed my classes as follows:


package food;

public abstract class Fruit {

public int x;
private int y;
public abstract int getX();
}


And,


import java.io.IOException;

import food.Fruit;

class Apple extends Fruit {

int x;

public static void main(String[] args)
{

System.out.println("Enter a value for X");
getX();
System.out.println(getX());

}


public int getX() {
int x = 0;
try {
x = System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
return x;
}
}


And get an error at the following two lines in the second code:

getX();
System.out.println(getX());


Asking me to change the modifier for getX( ) to "static", and when I do that, the colpiler askes to remove the staticfrom getX( ).

How can I go through this problem?

Thanks.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags. You're trying to call an instance method (non-static) from a static method (main). You need to instantiate an Apple.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, it's hard to answer your question since we don't know what you're trying to do. But I suspect you want to define your classes like this:

If you're just playing around with Java, then here's a tip: All instance fields, like "x" should be defined as private. Avoid re-defining an instance field in sub-classes because it gets confusing. Since you defined "x" in Fruit it is also automatically part of Apple, so there's no need to redefine it. In my code, there's no way to assign a value to x. There's no single "right way" to do this -- it depends on what "x" represents. You could initialize it as you define it or in Fruit's constructor, and, if you design calls for it, you could have a "setX(int x)" method.
 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much to everyone.
 
My pie came with a little toothpic holding up this tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic