#Static

38 messages · Page 1 of 1 (latest)

strong solstice
#

Hey could someone explain me what the "static" keyword does?
I tried making an array to store all instances of a class created in that array and someone told me to use static and it worked that way but i don't understand why

wicked loomBOT
#

This post has been reserved for your question.

Hey @strong solstice! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

glad token
#

static fields are persistent and don't get cleaned by GC

#

idk what exactly do you need tho

#

show code

strong solstice
#

@glad token so speaking about an array if an array is static in my class each time i create a new instance of that class a new array won't be created right?

visual bough
strong solstice
#

wait i will give an example

strong solstice
#

And i create 2 instances

package packag;

import java.util.Scanner;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        //NewAccount.Deposit(scanner.nextInt());
        arrayExample instance1 = new arrayExample();
        arrayExample instance2 = new arrayExample();
    }
}```
#

there won't be created 2 ArrayLists

#

there will be only 1 wich every intance of the arrayExample class will share?

visual bough
#

Yup

#

The variable instance is shared by all instances (and can even be accessed from outside any instance)

#

And as the **new ** that creates the ArrayList is only on the line that declares the static variable, only one will ever be created

strong solstice
#

but if it wasn't static every time i would create a new instance it will create a new arraylist asweel?

visual bough
#

Yep, which is desirable since the variable would be tied to said newly created instance

strong solstice
#

but is there a way to create my arraylist in my main function and in the arrayExample constructor to add to that arraylist?

visual bough
#

I mean, you're the programmer, you do what you want

strong solstice
#

so that way i don't need to use the static keyword?

visual bough
#

Like in your main() you could have:

arrayExample.instances = new ArrayList<>();
visual bough
#

Like you'd have a class/method in charge of creating and providing new instances. And whenever it creates a new one it adds it to the list.

strong solstice
#

isn't that what i am doing?

#

like the class is creating my array and the constructor is in charge of adding each instance to the array

visual bough
#

Here your class is taking charge of itself and keeps a static list where it adds the instances it created

#

If somehow the use of a static list isn't satisfactory to you, then the better approach is to have another class that's in charge of keeping track of the instances of this one. Then the original class is only in charge of its own construction and behavior, and the other in charge of keeping track of the instances

#

That's usually how it's done because it's flexible and can do whatever you need

strong solstice
#

and how i can do that from what i understand i just making a new class that i create an array into but how i add the instances to that array

visual bough
#

You make it so the new class is the one that all others must ask new instances to. The new class is the one that calls the constructor, and that adds to the list right after. Then it passes the new instance with return

strong solstice
#
package packag;

import java.util.ArrayList;

public class arrayExample {
    public arrayExample(){
        //
    }
}
package packag;

import java.util.ArrayList;
import java.util.Scanner;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args){
        ArrayList<arrayExample> instances = new ArrayList<>();
        arrayExample instance1 = new arrayExample();
        instances.add(instance1);
    }
}```
this way?
visual bough
#

More or less, except here you're doing it in your main(). main() is not centralized and can't be used by others

strong solstice
#

uhhh

strong solstice
delicate mesa
#

@strong solstice tip for you, checkout these #1051626377608171561. These can have answers to questions like this