0

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.

Solaire
  • 53
  • 1
  • 8
  • As well as `HEIGHT` and `NAME` being non-static, in your main method, `tree` should be replaced with `first_tree`. Note that this does not follow camelCase conventions. – bcsb1001 Dec 05 '14 at 16:10
  • Good point, didn't see it, thanks! – Solaire Dec 05 '14 at 16:23

3 Answers3

1

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;

Community
  • 1
  • 1
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
  • 1
    If you are removing `static`, why remove `final`? It is allowed to initialise `final` fields in a constructor. – bcsb1001 Dec 05 '14 at 16:11
  • yes you're allowed to do it, I'm not saying that you're prohibited from doing that, I didn't explained it properly. Thanks. PD: I removed "final" because I'm following the indications in the link I attached on how to use "final". That's all, you're allowed to use final if you consider necessary, but remember that if you want to include setters later for the class, you will not be able to do it. That's all. – Marcelo Tataje Dec 05 '14 at 16:20
  • Thank you for the answer! However, even if now the class Tree is correct and compile with no errors, the terminal still gives me those two errors, as it couldn't see it when compiling Evergreen. – Solaire Dec 05 '14 at 16:21
  • Try to clean and build your project again and recompile your classes. – Marcelo Tataje Dec 05 '14 at 16:27
  • I tried to recreate the classes with the package in Eclipse, and all works fine, so apparently, aside from some syntax error, the problem is with the java compiler of the terminal. I wish I could fix it one day. However, very thanks for the support! – Solaire Dec 09 '14 at 12:20
1

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;
    }
}
Community
  • 1
  • 1
dripto
  • 570
  • 1
  • 7
  • 17
  • Thank you for the answer! However, even if now the class Tree is correct and compile with no errors, the terminal still gives me those two errors, as it couldn't see it when compiling Evergreen. – Solaire Dec 05 '14 at 16:20
  • The errors in Evergreen might be there because, 1. You have initialized object with identifier(name) first_tree and you are trying to access an object with tree identifier. so, System.out.println(tree.getName()); will be System.out.println(first_tree.getName()); and 2. might be because you are trying to access a static method using object reference. static methods work in the context of class, not of objects. So, System.out.println(first_tree.getName()); will again be System.out.println(Tree.getName());. – dripto Dec 05 '14 at 16:22
  • I tried to recreate the classes with the package in Eclipse, and all works fine, so apparently, aside from some syntax error, the problem is with the java compiler of the terminal. I wish I could fix it one day. However, very thanks for the support! – Solaire Dec 09 '14 at 12:19
0

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());
    }
    }
outdev
  • 5,249
  • 3
  • 21
  • 38