Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Say i have a class 'Test' which has number of static methods.
class Test {
public static T BytesToInt<T>(){
//Logic
}
public static void Parse(string data){
//
}
}
I create large number of instances of Test class. Will these objects be garbage collected?
Yes. As long as nothing is holding a reference to them they will be collected
Yes, when any Test instance out of scope or no longer referenced it would be eligible for GC. In general, instance means object created with constructor for example new Test(), and resides in heap area. Not to be confused with static members of which are class method.
Refer static method section in JLS below for more details:-
https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.3.2
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
As someone who is learning Java I find it hard to pick whether I should create class with empty constructor or making all her methods statics.
If I’m having a class without properties that read files and doing operations on the data and Being called only one’s shout it be static or have empty constructor.
Because I need to call only one method from the class (and he calls the rest) should I make all methods static or should I call her by creating empty object ?
Actually it would be a private constructor, not empty as you don't want to instantiate the class. But here are a couple guidelines.
Create static methods that don't require accessing instance fields but do some computation. An excellent example of this is the Math.class
Instance methods are used when accessing instance fields and possibly changing then. Getters and setters are good examples. Sometimes private helper methods can be declared static.
But don't use static methods as the fundamental type or because they are easier to use (i.e. work in static or non-static context). They are contrary to the concept of OOP.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am making a minesweeper game. I want to reduce the number of flags in the inner class but I get this error ."local variables referenced from an inner class must be final or effectively final java".How can I solve this problem ?
You can't reference a variable from the outer class in the inner class if it's not at least effectively final. An effectively final variable is a variable that is guaranteed to never change.
However, you can wrap your variable. Any kind or wrapper is good.
You can transform 'flags' (or whatever you called your variable) into one element array and use it instead:
final int[] flags = {0};
Now you can update your variable from the inner class:
flags[0]++;
You can also use AtomicInteger or any other kind of wrapper.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I understand that in a static method, non-static members should be called with related objects, or there would be ambiguity.
If my understand is correct?
In Java, if a method uses a static member, why should itself be declared as static?
This is not true - a method that uses a static member does not need to be static itself.
I understand that in a static method, non-static members should be called with related objects, or there would be ambiguity.
If my understand is correct?
No.
When a member variable or method is static, it means that this member variable or method isn't part of, or doesn't work on one specific object of the class; it's shared by all objects of the class. The section Understanding Class Members in Oracle's Java Tutorials explains this in more detail.
Non-static methods work on a specific object, so if you call them from a static method, you have to call them on an object, since there is no current object (which this refers to) when you're in a static method.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why is it that all static methods and variables are accessible without any instance of class and non-static members need instances to get access.
Actually, there is a class object representing each class in the JVM. So, the line Why is it that all static methods and variables are accessible without any instance of class is incorrect.
The JVM creates class objects (different from class instances) representing classes.
Example : String.class, Class.class etc
When We create a class that means we are creating 'Bunch of Objects(instances) of Same type',for those objects,methods are remains same but data and memory location changes and it is unique for each object.But When We use 'Static' variable or Method ,It creates Only ONE COMMON COPY in whole program.So it is same for all instances/objects and changes done in static method/variable is visible to all objects. hence,we can use it directly or without instance of class.in same way,non static members are different with respect to every object,so we need instance of class for it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
myClass1:
public class myClass1
{
public myClass2 myclass2;
public void createsecondclass (String[] args)
{
myclass2 = new myClass2(this);
myclass2.dosomething();
}
myClass2:
public class myClass2
{
public myclass1;
public myClass2(myClass1 myclass1)
{
this.myclass1 = myclass1;
}
public void dosomething()
{
myclass1.another_object_that_could_be_placed_here.dosomething();
}
}
would this not make the code cleaner when trying to access a large amount of objects which all in one way or another are all instantiated under a single class? i ask because i am trying to learn libGDX and in the large assortment of class files that are made to handle each element of the game it just seems easier to pass my application listener down the line since the application listener contains the screen which contains the gameworld which contains the player and so on...
But the problem that i worry about is that by setting it to a variable in the object i am creating a myclass1 that contains a myclass2 that contains a myclass1 and so on. i worry that this might cause memory leaks and since my target is android, memory is a big concern.
if anyone has any thoughts on the subject, directly relevent or not i would appreciate the input. i am after all still learning.
thanks =)