#Static
38 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @strong solstice! Please use
/closeor theClose Postbutton 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.
Check out this StackOverflow question for details on the static keyword.
static makes field independent of class instance, turning it to a part of class itself
static fields are persistent and don't get cleaned by GC
idk what exactly do you need tho
show code
@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?
Technically a new array will be created every time you run into a line that creates a new array.
It all depends on where the array is created. But no new field will be created to host the array if it is a static field. There will be only one within the entire program, compared to nonstatic fields where there is one per object
wait i will give an example
let's say i have this class
package packag;
import java.util.ArrayList;
public class arrayExample {
static ArrayList<arrayExample> instances = new ArrayList<>();
public arrayExample(){
instances.add(this);
}
}
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?
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
but if it wasn't static every time i would create a new instance it will create a new arraylist asweel?
Yep, which is desirable since the variable would be tied to said newly created instance
but is there a way to create my arraylist in my main function and in the arrayExample constructor to add to that arraylist?
I mean, you're the programmer, you do what you want
so that way i don't need to use the static keyword?
Like in your main() you could have:
arrayExample.instances = new ArrayList<>();
For what you're describing, the best way to do it is that it's not the constructor that adds to the list
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.
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
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
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
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
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?
More or less, except here you're doing it in your main(). main() is not centralized and can't be used by others
uhhh
if you have time could you give me an example?
@strong solstice tip for you, checkout these #1051626377608171561. These can have answers to questions like this