Two different variables getting same value

2020-07-19 23:59发布

I am currently working on a small Java application and I ran into a problem. I create two different variables, but after I run the code, the first variable is getting the same value as the second one. They should be different.

Here is my custom 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;
    }
}

Here I am using the variables:

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 output is the following: http://imgur.com/a/Nu3N6

标签: java
5条回答
The star\"
2楼-- · 2020-07-20 00:24

When Defining an Entity class the class variable show be private period. Unless you want to access these variable statically, as in without having to instantiate the class or using getters and setter. If you use getters and setters as you have done above, and clearly made an instance of the class you want use ensure you don't use static access modifiers for the class variables.

The modified code is-as below. package StackOverflowProblemSets;

/** * Created by HACKER on 05/06/2017. * Two different variables getting 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());
}

}

查看更多
够拽才男人
3楼-- · 2020-07-20 00:29

In MyFile class, you declare these fields as static fields :

private static String path;
private static String name;

So you can assign to them a single value as a static field is shared among all instances of the class.

You should rather declare these fields as instance fields to have distinct values for each MyFile instance :

private String path;
private String name;
查看更多
劳资没心,怎么记你
4楼-- · 2020-07-20 00:30

First you want to know about static keyword:

  • Attributes and methods(member of a class) can be defined as static.
  • static members do not belongs to an individual object.
  • static members are common to all the instances(objects of the same class).
  • static members are stores in static memory(a common memory location which can by everybody).

Becauseof two member variables are static. Each objects share the values of these two variables(values are common for every objects).

private static String path;
private static String name;

Remove the static in both variables. Then each and every object will hold a individual values for these variables.

查看更多
ら.Afraid
5楼-- · 2020-07-20 00:30

You need to know about static and local variables.

Static variables of a class are such variables which are common to all instances of that class and are shared by all of the instances. E.g. 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 main method of 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 will be:

8 8

This is because the variable staticVar is shared by both c1 and c2. First when the statement c1.staticVar = 4 is executed, the value of staticVar for both c1 and c2 is 4. Then the statement c2.staticVar = 8 is executed to change the value of staticVar of both classes to 8.

So in your problem, you have to make your name and path variables non-static to give each of your myFile instances a different value of the variables.

I hope this helps.

查看更多
Juvenile、少年°
6楼-- · 2020-07-20 00:37

You problem is second file path is overlap of 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"));
查看更多
登录 后发表回答