Making parameter of main method as final [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 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.

Related

Static variables and instance block in Java [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 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.

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.

Constants in Interface [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I know there were a lot of questions about putting constants into interfaces, and that is a bad practice.
But I have a question, what if I have a class, where I want to define some sets of constants, but I want to have them in groups, is it a bad practice to have interface/class inside a class and define constants in there, so my constants are grouped?
e.g.
public class MyClass{
public final class A {
public static final String A = "a" ;
}
public final class B {
public static final String B = "b";
}
....
}
Yes you can have nested class to group the constants. It's really not a bad practice.
the question leads to a misleading. From my point of view the necessity to group constants is related to the fact that they regards different classes and so they must be putted int the correct classes; in other words this question can be an alarm about a not perfectly correct class design.

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

generics and static [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 9 years ago.
Improve this question
In this book that I'm using to brush up, there's a relationship between generic methods and the static keyword. It appears that generic methods require that keyword, but I don't see why it's required?
Here's what the book did:
static <T> void myFunction(T [] myArray) {
//......
}
There is no requirement that generic methods be static. See Section 8.4.4 of the Java Language Specification for details (including links to other relevant parts of the spec). At the same time, there's nothing wrong with a generic method being static (or vice versa), either. It all depends on the design requirements.

Categories

Resources