int cannot be dereferenced

mdoland
mdoland used Ask the Experts™
on
I have an application where I from the class with the main method want to set certain static values in a class X. X extends an abstract class Y. I can set a similair value in Y. What is wrong?

I get compilation errors saying:  int cannot be dereferenced

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
Can you post the code to make it more understandable?
Awarded 2011
Awarded 2011
Commented:
I tried this code (see below)
and it compiles without problem.


public class A_and_B {
    public static void main(String[] args) {
        X.aa = 2;
    }

}
abstract class Y {
    static int aa = 1;
    public abstract void goTo();
}
class X extends Y {

      public void goTo(){

      }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
Where I saw this kind of diagnostic cime up in compilation is when
you try to call the method on primitive type, like

int i;

String s = i.getName();

This cannot work because you cannot call methods on primitive type

Author

Commented:
Does this work? Should it work?
public class A_and_B {
    public static void main(String[] args) {
        X.aa = 2;
    }

}
abstract class Y {
    public abstract void goTo();
}
class X extends Y {
      static int aa = 1;

      public void goTo(){

      }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
It is the same code I posted? Did you change anything - I don't see any changes.
Yest, I tried it - it does compile, and should run also, why not?

Author

Commented:
I changed it slightly ... I put the aa in the other class.
Awarded 2011
Awarded 2011

Commented:
Of course this one also compiles wihout any problem -
this one is even less controversial than prvious versio
aa is in class X and you just change X.aa = 2 -
it is even more obvious than the previos case.

But they both compile without any problem.
Try for yourself.

public class A_and_B {
    public static void main(String[] args) {
        X.aa = 2;
    }

}
abstract class Y {
   
    public abstract void goTo();
}
class X extends Y {
               static int aa = 1;
      public void goTo(){

      }
}

Open in new window

Are the class with your main method, X, and Y all in the same package?  You've declared the static variable as package-private (no modifier) so only classes in the same package will be able to access it.  If you change it to public, it should start working.

It would also be better to keep the actual variable private and expose public static methods to modify/access it as needed.  In general, that is a better approach than the "global variable" you are creating.  Also, if Z extends Y as well -- should it share the same aa value as X?  If so, then keep aa in Y, otherwise, you'll need a separate counter for each set of subclasses.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial