writing a static method to set counter java?

onesnapthanos :

I am pretty new to coding, I want to write a static method to set a count value to 1000.

I tried doing things like

public static  int setCounter(){
        return 1000;

    }

When try to use this method in another to increment the count

such as someVariable = symbolic constant + setCounter()++; return someVariable, it gives me an unexpected type error, required variable, found value. What am i doing wrong?

edit*

edit*

private String getTicketNumber(){
    ticketNumber = TICKET_PREFIX + resetCounter()++;
    return ticketNumber;

 public static int resetCounter(int counter){
        counter = 1000;

    }

i want to call this counter to increment it in another method

flakes :

An static property is associated with the definition of the class, and there is only ever one definition of the class. An instance is created whenever you use the new keyword on the class. All instances are keep their own private data from the static class. When using a class instance you can access the static variables but not the other way around.

You use the term constant, but I think you might have a bit of a misunderstanding of what that implies. Constants are often declared as static final values that are immutable (they can never be changed). You should not be able to modify a constant. A static value can be mutable if you so desire, and this is what we would sometimes refer to as a stateful singleton.

Consider these two examples... one modifies an instance variable, the other a static variable.

class InstanceFoo {
    int i = 1000;
    int resetCounter() {
        i = 1000;
        return i;
    }
    int getAndIncrement() {
        return i++;
    }
}

class StaticFoo {
    static int i = 1000;
    static int resetCounter() {
        i = 1000;
        return i;
    }
    static int getAndIncrement() {
        return i++;
    }
}

In the first example you need to use an instance to access the variables.. i.e. you need to instantiate it with new:

InstanceFoo foo = new InstanceFoo();
System.out.println(foo.resetCounter()); // 1000
System.out.println(foo.getAndIncrement()); // 1000
System.out.println(foo.getAndIncrement()); // 1001
System.out.println(foo.getAndIncrement()); // 1002
System.out.println(foo.resetCounter()); // 1000

In the second you access the static value. Statics can be referred to by to the class definition:

System.out.println(StaticFoo.resetCounter()); // 1000
System.out.println(StaticFoo.getAndIncrement()); // 1000
System.out.println(StaticFoo.getAndIncrement()); // 1001
System.out.println(StaticFoo.getAndIncrement()); // 1002
System.out.println(StaticFoo.resetCounter()); // 1000

In your example you are trying to increment the counter by doing resetCounter()++. This will not work for a separate reason entirely from being static or instance. Primitive values in Java (like ints, doubles, floats, etc) are pass by value, not pass by reference.

In a very simplistic sense, this means that once you return a primitive from a method like resetCounter, you are actually passing a copy of the value. You then incremented the copy, but the value associated with the class remains the same (because you incremented only the copy of the variable). You need to call the postfix operator ++ on the variable itself, not the value returned by the method. i.e. If I have

class StaticFoo {
    static int i = 1000;
    static int get() {
        return i;
    }
}

System.out.println(StaticFoo.get()++); // prints 1000 and adds 1. the copy is then destroyed
System.out.println(StaticFoo.get()++); // prints 1000 and adds 1, the copy is then destroyed
System.out.println(StaticFoo.i); // prints 1000
System.out.println(StaticFoo.i++); // prints 1000 and now a postfix is applied to the static variable
System.out.println(StaticFoo.i++); // prints 1001 and another postfix is applied to the static variable
System.out.println(StaticFoo.get()); // prints 1002 because two postifx operators were applied to StaticFoo.i.

Hope this helps you get started.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216619&siteId=1