Week 46 — What does the `static` keyword do in Java?
Question of the Week #46
What does the
static
keyword do in Java?13 Replies
When creating a variable or method in a class, it typically is an instance variable/method.
Instance variables/methods are part of an object and can only be called with an object of the said class.
Different objects have different values of instance variables
On the other hand, static variables are part of the class and not part of the object.
No matter how many objects of a class exist, a static variable has the same value.
When accessing a static variable or method, one doesn't need a reference to an object of that class.
If a non-static method calls another non-static method (or accesses a non-static variable), it doesn't need to provide an object of the class since it can use the object which is used for calling the method originally.
However, a
static
method cannot call another non-static method (or access a non-static variable) like this since it needs an object to do so.
📖 Sample answer from dan1st
The static keyword is a non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class.
Submission from Wisdamy#3452
Static expressions work only in one place and only once. This is the simplest version. A static variable does not depend on any class structure. It is kept in one place in memory.
It is the same as the working states of static methods. They manage static data. And they are also used as input points. But we can call them inside normal methods.
public class JavaStaticExample{
static int counter;
public static void main(String[] args) {
JavaStaticExample.counter++;
System.out.println(JavaStaticExample.counter);
//Since the memory is not cleared, it counts over the same
JavaStaticExample.counter++;
System.out.println(JavaStaticExample.counter);
}
}
Submission from ermanetwork
It is a access modifier keyword used to call a method without instantiating a new type of the class.
Submission from hahahahahahehehe
It is used to share a common variable and/or methods across all instances of a certain class.
Submission from thespiky.hedgehog
Allows the specified class, method, or field to be referenced from different scopes.
Submission from cvs0.
Definition:
The static keyword is used on variables, methods, and classes to denote that they are a member of the class, instead of an instance of a class (called on by an object). Basically objects aren’t used to call on static things in the class, or objects can’t be created if it’s a static class.
Code Examples:
The Math Class is a static class, and you can’t create an object of the Math Class. All of the parts of the Math Class are also used by saying Math.[partName] instead of [objectName].[partName]
Submission from justelectro1
the "static" keyword can make many instances share the same atribute or method. I'll make an example;
class Student{
String name;
static String stream;
public Student (String name, String stream){
this.name = name;
this.stream= stream;
}
class School {
public static void main (String args []){
Student s1 = new Student ("john","stream1");
Student s2 = new Student ("josh","stream3")!
So, in this example, I created a class named "student", which atributes are name and stream, and I made two Student objects, the first, it's name is john and the stream is stream1; And the second object it's name is Josh and the stream is stream3...these two student has a different name , but the once we declare that the stream is static, I can say : I have many students with different names but the has something in common, the stream...
So int the the construtor of the first tudent , when I declare that he is in the stream1, It saying...that the next following students will be in the same stream, but in the second student when a declare that he is in the stream3, it was saying that the second and first one has changed the stream from stream1 to stream3.
Submission from blaze380
The static keyword allows you to access a field or a method without an instance of the class, using just it's name
Submission from laur.gg
Create a method and instead of creating an object u just call it like u call functions in C
Submission from senbaj
Static keyword simply means that you can access one class' methods without having to instantiate an object first before being able to use them.
For example consider these 2 classes:
In this class we make 2 methods which return
animal
and name
respectfully with hardcoded values of "Dog" and "Lee"
In this class we have a constructor which takes the Animal
and name
and methods associated with the variables.
In the main method, we first of all instantiate a new object called a
with values "Cat" and "Jake". Which then we just get them and print them.
In the second part, we are accessing the AnimalStatic class without having to instatiate a new object.
This will print:
⭐ Submission from akubsy