Reputation: 11
I have some problems by calling a Variable of a separate .java Class in an other .java class.
I tried to declare the Variables in the Variables.java file and use the same two variables in the other .java file. But it doesn't work.
Variables.java:
package de.cfe.base;
public class Variables {
public static int int1;
public static int int2;
}
and
CallingTheVariables.java:
package de.cfe.base;
public class CallingTheVariables {
public static void main(String[] args) {
int1 = 1;
int2 = 10;
}
}
Upvotes: 1
Views: 3653
Reputation: 14278
try below:
Variables.int1 = 1;
Variables.int2 = 10;
or you can do a static import.
import static de.cfe.base.Variables.*;
The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:
import static java.lang.Math.PI;
or en masse:
import static java.lang.Math.*;
when should you use static import?
Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability;
if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.
Upvotes: 0
Reputation: 1846
You must reference the variables correctly. Use
Variables.int1 = 1;
Variables.int2 = 10;
since they're static, or if you want to create instances of them, remove their static modifiers, then use;
Variables v = new Variables();
v.int1 = 1;
v.int2 = 10;
Upvotes: 0
Reputation: 38195
public class CallingTheVariables {
public static void main(String[] args) {
Variables.int1 = 1;
Variables.int2 = 10;
}
}
Upvotes: 0
Reputation: 178243
When you say just int1
and int2
, Java assumes that they are defined already and in scope in the class. But they aren't defined in CallingTheVariables
. You must qualify your reference with the class on which they're defined:
Variables.int1 = 1;
Variables.int2 = 10;
An advanced alternative is the static import (scroll down in that page). In the class to reference the variables, place this above the class:
import static de.cfe.base.Variables.*;
Then you could refer to the variables with their simple names as you have it already:
int1 = 1;
int2 = 10;
Upvotes: 4
Reputation: 30300
Try this:
Variables.int1 = 1;
Variables.int2 = 10;
int1
and int2
"belong" to Variables
, so you can only access them through Variables
.
Upvotes: 2