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.
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 8 years ago.
Improve this question
I am having great difficulties understanding this construct
public class MyBucket extends bucket {
private double var1;
private MyShovel Shovel; <- this is the problem here
public MyBucket(int volume, MyShovel Shovel) {
Some code here
}
}
I cannot understand what private MyShovel Shovel is in Java terminology. It is a class defined in my project but I cannot determine what it is called in the Java world. Can someone please help me define it so I can research what it is. This is production code and it works. Just cannot figure out the official Java name for that construct.
Shovel is a (private) member. Its type is MyShovel. This means that every instance of MyBucket contains a reference to a MyShovel object, which can be referenced only from within the class, by the name Shovel.
Note that this declaration does not follow java naming conventions. In the proper conventions it would have been called myShovel, in lowerCamelCase.
Shovel is just a variable. It is an instance of MyShovel. MyShovel would be a Class that represents a virtual shovel. Since it is a global variable, or field, in MyBucket this means that each MyBucket also would have MyShovel inside of it.
In the code you provided it is undefined and has no value. Since it is private only methods in MyBucket can access it. The MyBucket constructor takes a MyShovel parameter and is most likely what Shovel is set to.
You can think of it like this:
Every MyBucket must have a MyShovel inside of it to make it work. So when creating a MyBucket you put a MyShovel inside of it. Shovel is the space that that particular MyShovel will be stored.
As others mentioned it should be named "shovel" for naming convention.