Two different variables get the same value
I am currently working on a small Java application and I am having a problem. I create two different variables, but after running the code, the first variable gets the same value as the second. They must be different.
Here is my own file class:
public class MyFile {
private static String path;
private static String name;
private static final String FILE_SEPARATOR = "/";
public MyFile(String path) {
System.out.println(path);
this.path = "";
this.name = "";
this.path = /*FILE_SEPARATOR*/path;
String[] dirs = path.split(FILE_SEPARATOR);
this.name = dirs[dirs.length - 1];
}
public static String getPath() {
return path;
}
public static String getName() {
return name;
}
public String toString() {
return "Path: " + path + ", Name: " + name;
}
}
I am using variables here:
MyFile modelFile = new MyFile("res\\model.dae");
MyFile textureFile = new MyFile("res\\diffuse.png");
System.out.println(modelFile.toString());
System.out.println(textureFile.toString());
The conclusion is as follows: http://imgur.com/a/Nu3N6
source to share
In a class, MyFile
you declare these fields as fields static
:
private static String path;
private static String name;
This way you can assign one value to them as the field static
will be used for all instances of the class.
You should rather declare these fields as instance fields to have different values ββfor each instance MyFile
:
private String path;
private String name;
source to share
First you want to know about a
static
keyword:
- Attributes and methods (class member) can be defined as
static
.static
members do not belong to a separate object.static
are common to all instances (objects of the same class).static
members are stores in static memory (a shared memory location available to everyone).
Because of the two member variables static
. Each object shares the values ββof these two variables (the values ββare common to all objects).
private static String path;
private static String name;
Remove static
in both variables. Then each object will contain separate values ββfor these variables.
source to share
When defining an Entity class, the class variable appears as a private period. Unless you want to access this variable statically, as without having to instantiate the class or use getters and setters. If you are using getters and setters as you did above and have explicitly instantiated the class you want to use, make sure you are not using static access modifiers for class variables.
Modified code - as shown below. the StackOverflowProblemSets package;
/ ** * Created by HACKER on 05/06/2017. * Two different variables that get the same value * / public class MyFile {
private String path;
private String name;
private static final String FILE_SEPARATOR = "/";
public MyFile(String path) {
System.out.println(path);
this.path = "";
this.name = "";
this.path = /*FILE_SEPARATOR*/path;
String[] dirs = path.split(FILE_SEPARATOR);
this.name = dirs[dirs.length - 1];
}
public String getPath() {
return path;
}
public String getName() {
return name;
}
public String toString() {
return "Path: " + path + ", Name: " + name;
}
public static void main(String[] args) {
MyFile modelFile = new MyFile("res\\model.dae");
MyFile textureFile = new MyFile("res\\diffuse.png");
System.out.println(modelFile.toString());
System.out.println(textureFile.toString());
}
}
source to share
You need to be aware of static and local variables.
Static class variables are variables that are common to all instances of that class and are shared by all instances. For example. if i have a class:
public static class myClass{
public static int staticVar;
//Constructor
public myClass(){
staticVar = 0;
}
}
and then I have the following code in a method of main
another class:
public static void main(String args[]){
myClass c1, c2;
c1 = new myClass();
c2 = new myClass();
c1.staticVar = 4;
c2.staticVar = 8;
System.out.println(c1.staticVar + " " + c2.staticVar);
}
then my output would be:
8 8
This is because the variable staticVar
is shared both c1
and c2
. First, when the operator is performed c1.staticVar = 4
, the value staticVar
of and c1
and c2
is equal to 4. Then, the operator c2.staticVar = 8
to change the value staticVar
of both classes to 8.
So in your problem you have to make variables name
and path
non-static ones to give each of your instances a myFile
different value for the variables.
Hope this helps.
source to share
The problem is that the second file path is overlapping the first file path. So check this code:
MyFile modelFile = new MyFile("res\\model.dae");
MyFile textureFile = new MyFile("res\\diffuse.png");
System.out.println(new MyFile("res\\model.dae"));
System.out.println(new MyFile("res\\diffuse.png"));
source to share