#What does Static mean??
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
In Java, the keyword "static" is used to declare a variable, method, or nested class as belonging to the class itself, rather than to instances of the class.
When a variable or method is declared as static, it means that it is shared among all instances of the class and can be accessed without needing to create an instance of the class. This also means that static members are initialized only once when the class is loaded into memory.
For example, if you have a static variable in a class, changing its value in one instance will reflect that change in all other instances of the class.
Similarly, static methods can be called directly on the class itself without needing an instance of the class. This is useful for utility methods or methods that do not depend on instance variables.
It is important to note that static members cannot access non-static members directly, as they do not have access to a specific instance of the class.
In summary, using "static" in Java allows for variables and methods to be shared across all instances of a class and can be accessed without needing an instance of the class.
Hi
It defines the how to access a method or variable
If you have e.g. a vehicle class with a number of seats variable than the number of seats variable is a non static variable because every vehicle has its own number of seats
However if you want to count how Many vehicles are constructed you can add a counter in a static way to the vehicle class that counter exist only one time for all vehicle instances and is the same for all instances
For methods it’s there is only one argument to say
Static methods can’t depend on instance variables
Let’s say our vehicle has a method hasFreeSeats() depends on the number of seats and this is different for each instance of an vehicle
But the question of how Many vehicles are constructed uses the static variable and do not need the object
In a point of syntax
Class.method is static
Object.method is non static
Constants are static (and final other key word )
Hope it helps
@open pulsar There are other answers, but I explain it on chapter 37 of this book
Book teaching how to write modern and effective Java.
so good news, I think thats a really decent explanation
bad news, it makes the most sense if you learned other topics by following the book
Because you will get yelled at for using a non-final static field no matter what you do
I like your writing
it's like getting work experience without working 😄
However if you want to count how Many vehicles are constructed you can add a counter in a static way to the vehicle class that counter exist only one time for all vehicle instances and is the same for all instances
Not quite, the variable belongs to the class, not shared among instances of the class. Even if 0 instances exist you can still use the static variable.
For methods it’s there is only one argument to say
Static methods can’t depend on instance variables
No, that the method acts upon the class/belongs to the class. That you can't use instance variable is a consequence of that.
(simplisticly)
I'm a Person. My name is part of me (name is non-static)
Whereas the DEFAULT_RATING for the class of Person (MOSTLY_HARMLESS) is a property of the type (DEFAULT_RATING is static).
Each Person could have a rating (non-static) of their own, and we could use the constructor to initialise it from the default.
enum Rating { HARMLESS, MOSTLY_HARMLESS, NOVICE, COMPETENT, EXPERT, ... }
class Person {
public static final Rating DEFAULT_RATING = Rating.MOSTLY_HARMLESS; // belongs to the class
private String name; // each person has one of these
private Rating rating; // and these
public Person(String name) {
this(name, DEFAULT_RATING);
}
public Person(String name, Rating rating) {
this.name = name;
this.rating = rating;
}
public String name() {
return name;
}
public Rating rating() {
return rating;
}
}
So we can say
- "What is the default rating for a Person?" -
Person.DEFAULT_RATING - "What is taldennz's rating?" -
taldennz.rating()
Thank you so much everyone! This really helped! I think I understand now.
Have a continued good day! 🙂
// GamerBreeze
Wait So Static is null or 0 depending on whether it's a string or int?
static is nothing to do with whether a value is a null or a 0. That is determined by the data-type.
With String s, s is a reference that may be null or refer to an object-type assignable to String.
With int i, i is a primitive int and can contain any 32bit int value.
thats all fields when uninitialized
If s is a static field, it is null until initialised.
i.e. not given an explicit starting value
If i is a static field, it is 0 until initialised.
The same goes for when they are non-static fields.
However a local variable, such as declared in a method does not have a default value - you must assign it a value before it can be read.
Null or 0 until you give them I value?
yes. 0 if its int-ish, false for booleans, null for everything else
when they are fields of a class (static) or an object (non-static).
public void m() {
String s;
}
s is not null here. since s is a local variable (it has no defined value until it is assigned)
Book teaching how to write modern and effective Java.
Book teaching how to write modern and effective Java.
i do reccomend going from the start - you might need a little bit of help getting the code in this to run, but everything is explained
Ok thanks for all the help