Static variables and instance block in Java [closed] - java

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 1 year ago.
Improve this question
When initializing an instance variable in a static block, it's throwing compilation error (so only static variables are declared in static block). However, my program is executing without any problem when I'm initializing static variable in an instance initializer block. So, is it OK to initialize static variable in instance block?

It's possible to do this, but usually not desirable. The instance initializer block will run whenever a new instance is created, and overwrite any existing value in the static fields.

Related

How do I change the variable value in the inner class [closed]

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.

Do classes with static methods get Garbage collected? [closed]

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

Get file name from TUI class to different class [closed]

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 6 years ago.
Improve this question
Currently working on a tui application to prompt the user to enter a file name and then I need to get that name from the TUI class over to a model class. Every thing I try gives me the error "Cannot make a static reference to the non-static method getFileName() from the type TUI." Any suggestions on how I should go about the code? I know I haven't supplied a ton of information but I'm not completely sure what information is pertinent.
The problem is that your method getFileName() is not static, so it belongs to an object and you're trying to access it though the class and not an object. If it's the same fileName for every TUI object you just need to make the field fileName and the method getFileName() static.
public static void getFileName() {
If not you have to call the method on an object with the dot operator
TUI tui= new TUI();
TUI.getFileName();

java: usage of instance keyword [closed]

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 8 years ago.
Improve this question
i came across to a java code with like this
public class TestClass{
private static volatile TestClass instance = null;
///...............
}
What is the use of instance and volotile in java, and i don't know why do we need to explicitly give null value to class.
This variable is meant to be used in a threadpool.
here is the definition of the volatile keyword:
http://www.javamex.com/tutorials/synchronization_volatile.shtml

Making parameter of main method as final [closed]

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 9 years ago.
Improve this question
i saw a different signature of main() i.e.
public class Demo {
public static final void main(final String[] args) {
.............
.............
}
}
What is the benefit to make main()'s argument as final. please explain!
Method parameters in Java are local variables. Declaring them as final allows you to access them from inner anonymous classes. Other than that, it can help prevent potential bugs that happen when you change parameter values.
So in your example code, using final String[] args will work just fine.
String[] args is just a variable declaration, it corresponds to the command line parameters, so it can be final.

Categories

Resources