0

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?

4 Answers 4

4

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 (i.e. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification. As for the contradicting parts, that's the very reason why I came to this website and seek for advice, specially considering that I've read in different topics about similar "issues" that non-static access is discouraged
0

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.

1 Comment

Ok, thanks for the clarification. I think I might have misread when reading about non-static access. Apparently people went crazy whenever someone tried to do that, but reading your answer, I could conclude that they were using static methods. Thanks again.
0

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.

Comments

0

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.

Update: 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.