aspose file tools*
The moose likes Beginning Java and the fly likes Why am I getting this error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why am I getting this error?" Watch "Why am I getting this error?" New topic
Author

Why am I getting this error?

Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
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.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
It's probably because you are trying to define the getX() method inside the main() method.

John.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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

Joined: May 22, 2008
Posts: 138
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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Nov 06, 2000
Posts: 177
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

Joined: May 22, 2008
Posts: 138
Thank you very much to everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why am I getting this error?