-1

It is possible to get a public variable (let's call this "var") from class A in package Aa from class B in package Bb by doing

package Bb;

import Aa.A; public class B { int neededVar = A.var }

but how do I access "var" if A is not inside any package?

Edit: This question is different from this link. That question is asking how to access a variable that is in a class that is ALSO in a package from a class that is in a different package.

Illiyan
  • 163
  • 1
  • 1
  • 6
  • 3
    If there is no specified package for a class it's placed in `default package` and you can't import classes from `default package`. – whatamidoingwithmylife Jan 12 '18 at 05:39
  • @mjsoft It's not a duplicate. If you read the link, the asker was asking how to access a variable in a package from a package. Hence why I bolded NOT in my question title. – Illiyan Jan 12 '18 at 05:45
  • One of these ought to be a duplicate then: https://stackoverflow.com/questions/2193226/how-to-import-a-class-from-default-package , https://stackoverflow.com/questions/2030148/whats-the-syntax-to-import-a-class-in-a-default-package-in-java :D – user2864740 Jan 12 '18 at 05:49
  • 1
    @user2864740 Ah I see. I didn't know of the term "default package" as to why it did not show up when I searched for it. – Illiyan Jan 12 '18 at 05:53
  • Is `var` a field of an *instance* of `A`, or is it a (`static`) field of the *class* `A`? – Bohemian Jan 12 '18 at 05:55
  • @Bohemian static – Illiyan Jan 12 '18 at 05:56
  • @Illiyan then what's the problem here? If you have `public static int var = 42;` then your code should work. Are you getting a compile error? If so, what is it? – Bohemian Jan 12 '18 at 05:58
  • @Bohemian It not so much a compile error? Initially, I thought it would be similar to accessing a variable from another package so I tried A.var (this is inside of class B) but A is an "unrecognized symbol" according to the ide. – Illiyan Jan 12 '18 at 06:05
  • Does the line `import Aa.A;` compile OK? If so, I don't know what the problem could be. – Bohemian Jan 12 '18 at 06:10
  • Sorry, it may have been confusing when I asked the question. Class A is NOT in package Aa - Class A is not in any package (except the default package which I now know of). I was trying to clarify that my question was not the same as the top google search result. – Illiyan Jan 12 '18 at 06:14
  • 1
    Class A (if is not in a package) will most definitely be in the . But you cannot import classes from the default package and in general this package should be avoided. Paragraph 7.5 : https://docs.oracle.com/javase/specs/jls/se6/html/packages.html – Stamatia Ch Jan 12 '18 at 06:29
  • Why not move your class to its own package? – Stamatia Ch Jan 12 '18 at 06:30

1 Answers1

1

*It is possible to get a public variable (let's call this "var") from class A in package Aa from class B in package Bb* - this means that both classes ARE in the packages. So to access to var you need an instance of class A and to get var contained in this instance. So you could do something like this in class B:

A objA = new A();
int neededVar = objA.var;

But if the class you need to use in package Bb isn't contained in any package specified by you with the words package... in the very beginning (not counting comments) of Java file containing it , it is put into default package. You cannot use classes contained in default package in any other classes than contained in default package too.

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21