Solaire
Solaire

Reputation: 53

Java - Can't see classes inside a package

I tried to create a package with some classes in it, but seems like I have some visibility problem.

This is my first class:

package trees;

public class Tree{

private static final int HEIGHT;
private static final String NAME;

public Tree(){
    HEIGHT = 5;
    NAME = "Basic Tree";
}

public Tree(int a, String n){
    HEIGHT = a;
    NAME = n;
}

public static int getHeight(){
    return HEIGHT;
}

public static String getName(){
    return NAME;
}
}

And this is my second class:

package trees;

public class Evergreen{

public static void main(String[] args){
    Tree first_tree = new Tree(20, "Fir");

    System.out.println(first_tree.getName());
    System.out.println(first_tree.getHeight());
}
}

But when I compile, the terminal gives me these errors:

Evergreen.java:6: error: cannot find symbol
    Tree first_tree = new Tree(20, "Fir");
    ^
symbol:   class Tree
location: class Evergreen

Evergreen.java:6: error: cannot find symbol
    Tree first_tree = new Tree(20, "Fir");
                      ^
symbol:   class Tree
location: class Evergreen

The two classes are in a folder called "trees". I tried to insert a "implement trees.*" into the Evergreen class but nothing change. I'm using Mac Maverick, compiling with the terminal and Java is up to date.

Am I doing something wrong?

Thank you for the help.

Upvotes: 0

Views: 1700

Answers (3)

Marcelo Tataje
Marcelo Tataje

Reputation: 3871

Your constructor is not accessible due to compilation errors, basically because you defined your variables adding the modifier: final plus the static keyword,

When you do that, you need to specify a default value, else you will get a compilation error.

In your constructor you're assigning a value to your final variables, which is not allowed in the way you're declaring your variables before the constructor, every variable defined as final should have that value, that's the way you define constants for your code.

I will recommend you to check both concepts for final and static modifiers:

static

final

If you're trying to create a simple class to create instances of it and use their properties, you should just remove static and final modifiers, like:

public class Tree {

    private  int height;
    private  String name;

    public Tree() {
        height = 5;
        name = "Basic Tree";
    }

    public Tree(int a, String n) {
        height = a;
        name = n;
    }

    public int getHeight() {
        return height;
    }

    public String getName() {
        return name;
    }

}

Also, try to follow code conventions for defining variables, if the variables are constants, for sure you can write them in UPPERCASE, if not, you should write it with lower camel case: lowerCamelCase; for instance: double baseSalary.

Then you can define constants for your class, something like: private static final int MAX_TREE_HEIGHT = 100;

Upvotes: 1

dripto
dripto

Reputation: 619

It is not a visibility problem. Basically what is happening is you have variables HEIGHT and NAME which are final variables. According to the Java final variable contract mentioned here,

You are always allowed to initialize a final variable. The compiler makes sure that you can do it only once.

There are 2 issues in your code,

  1. You are not providing any value when you are initializing the final variables.
  2. You are trying to assign some values into the final variable in a later point of time

Both of these are in violation of the contract mentioned above.

You can try modifying the code by removing the final from the variable modifiers.

package trees;

public class Tree {

    private static int HEIGHT;
    private static String NAME;

    public Tree() {
        HEIGHT = 5;
        NAME = "Basic Tree";
    }

    public Tree(int a, String n) {
        HEIGHT = a;
        NAME = n;
    }

    public static int getHeight() {
        return HEIGHT;
    }

    public static String getName() {
        return NAME;
    }
}

Upvotes: 1

outdev
outdev

Reputation: 5502

Try

package trees;

public class Tree {

    private final int HEIGHT;
    private final String NAME;

    public Tree() {
        HEIGHT = 5;
        NAME = "Basic Tree";
    }

    public Tree(int a, String n) {
        HEIGHT = a;
        NAME = n;
    }

    public int getHeight() {
        return HEIGHT;
    }

    public String getName() {
        return NAME;
    }
}


package trees;

    public class Evergreen{

    public static void main(String[] args){
        Tree first_tree = new Tree(20, "Fir");

        System.out.println(first_tree.getName());
        System.out.println(first_tree.getHeight());
    }
    }

Upvotes: 0

Related Questions