Avatar of mdoland
mdoland
 asked on

int cannot be dereferenced

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

Java

Avatar of undefined
Last Comment
gordon_vt02

8/22/2022 - Mon
for_yan

Can you post the code to make it more understandable?
ASKER CERTIFIED SOLUTION
for_yan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
for_yan

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

mdoland

ASKER
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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
for_yan

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?
mdoland

ASKER
I changed it slightly ... I put the aa in the other class.
for_yan

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.