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.
Related
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.
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 4 years ago.
Improve this question
private final EventManager eventManager;
private final DateManager dateManager;
private final UserManager userManager;
What is the name of this construction? Is this object or something else?
EventManager, DateManager, UserManager are the names of classes.
It's called field declaration. You are declaring that these [private, final] fields exist, with those names and types.
Declaration of constant, or immutable, fields of types EventManager, DateManager and UserManager. Fields declared final can only be set once, by a constructor, they can't be modified after that.
You are declaring fields (class local variables). They are uninitalized (the objects don't exist yet, so no constructor has been called). They are set as final, meaning that they can only be assigned a value once, and you have not done that yet (so you have to do it in the construct).
If you wanted to define these AND call their constructor it would look like this
private final EventManager eventManager = new EventManager();
That would call the default constructor if it has one and initialized the field. Again, since the field is marked final and since in the above example it is being declared with a value, you can not set this value again, not even in the constructor.
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've got an object that is interface, let's call it MyInterface something.
My interface is empty and is being implemented by two classes.
First on has one variable int x, and the second one two: int x, int y.
Variables are private but they've got "get" methods and i know them.
Is it possible to get to the variable of a class just by using "something"?
No, it's not possible (excluding reflection magic), unless you add a getX() method to MyInterface. Then, it's easy and it has the nice side-benefit of being a correct design.
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.