简体   繁体   中英

Java initializing object - Static vs non-Static access

I haven't been coding anything for a while now and I decided to practice a bit. I came up with this issue at the very beginning of my program and I spent last night trying to figure out or find a way around this problem but I didn't get any expected results.

First, let's see the class:

public class Task {
private static int priority;
private static int taskTime;
private static boolean solved;

public void setPriority(int p){this.priority = p;}
public void setTasktime(int t){this.taskTime = t;}
public void setSolved(boolean s){solved = s;}

public int getPriority(){return this.priority;}
public int getTaskTime(){return this.taskTime;}
public boolean getSolved(){return this.solved;}

public Task(int p, int t){
    this.priority = p;
    this.taskTime = t;
    this.solved = false;
}

public static class ComparePriority implements Comparator<Task>{
    @Override
    public int compare(Task t1, Task t2){
        return Integer.compare(t1.getPriority(), t2.getPriority());
    }

}
}

Now, this is the piece of code I am trying to run:

public class main {

public static void main(String[] args) {            
    Task t1 = new Task(20,1);
    Task t2 = new Task(13,2);
    Task t3 = new Task(10,5);
    ArrayList<Task> t = new ArrayList<Task>();
    t.add(t2);
    t.add(t3);
    t.add(t1);
    System.out.println("List size: " + t.size());
    System.out.println("T1 object's priority: " + t1.getPriority());
    System.out.println("T2 object's priority: " + t2.getPriority());
    System.out.println("T3 object's priority: " + t3.getPriority());


    for(int i=0;i<t.size();i++){
        System.out.println("Current object task time: "+ t.get(i).getTaskTime());
        System.out.println("Current index:" + i);
    }

    Collections.sort(t, new Task.ComparePriority());

    for(int i=0;i<t.size();i++){
        System.out.println("Current object task time (post sort): " + t.get(i).getTaskTime());
        System.out.println("Current index: " + i);
    }
}

I understand that these attributes were defined in a static way and I should be accessing them as Class.method();

If I were to instantiate 3 objects (as used as example up above), is there any way to access them statically but still get every piece of information read from the object to be unique instead of "the same"?

Also why is accessing them non-statically discouraged?

You are asking for contradicting things.

access them statically

contradicts

still get every piece of information read from the object to be unique instead of "the same"

If you wish the former, you should expect the same value to be seen by all instances.

If you wish the latter, don't define them as static.

As for accessing them non-statically, as far as I know it makes no difference. They only reason I'd avoid accessing static members non-statically (ie by dereferencing an object of that class) is readability. When you read object.member , you expect member to be a non-static member. When you read ClassName.member you know it's a static member.

When a static method is called, it deals with everything on the class-level, rather than the object-level. This means that the method has no notion of the state of the object that called it - it will be treated the same as for every object that calls it. This is why the compiler gives a warning. It is misleading to use a static method with the form a.Method() because it implies that the method is invoked on the object , when in the case of static methods, it is not. That's why it's bad practice to call a static method on an instance of an object.

Accessing them non-statically is not discouraged at all in programming, but actually encouraged. If you want to access things non-statically, try putting that code in another class in a method. Then in this class put

public static void main(String[] args) { 
AnotherClass ac = new AnotherClass();
ac.initializeProcess()
}

I don't know why people like downgrading questions, I guess they think they were never new at all.

Static objects don't get thrown in the garbage-collector too quickly but it is saved as long there are references to it; 静态对象不会太快地扔到垃圾收集器中,但是只要有引用,它就可以保存。 static objects can cause memory issues

I think you misunderstand the difference between class variable and static variable. Program has only one value for static variable mean while every object has it own value for a class variable. It is also considered misleading to use this with static members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM