Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I see enum usage like below.
public enum MyEnum {
ENUM1(1),
ENUM2(2),
ENUM3(3);
private int enumValue;
MyEnum(int i) { this.enumValue = i; }
public int getEnum() { return this.enumValue; }
}
// this is the call
MyEnum.ENUM1.getEnum()
When above call is made, constructor of the MyEnum is called three times. All I want to take is a value but three instances are created! Isn't this a bad thing or am I doing something wrong?
EDIT: What I understand is that when the second call is made MyEnum.ENUM3.getEnum(), no more new instance is created.
You have three enum constants
ENUM1(1),
ENUM2(2),
ENUM3(3);
For each constant, the constructor needs to be invoked. This is normal behavior.
The constants are all initialized when the enum class is loaded and initialized.
The three instancee ENUM1, ENUM2 and ENUM3 are created - due to your declaraton of MyEnum when the class is loaded. Your call simply retrieves the int associated with ENUM1.
This is the way Java's enum is defined. You can't change it.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
If we are not adding any access specifier to method by default it is of default type.
but we are adding default keyword then it is giving me error, like we can have default method in interface only. I am aware about the default method in functional interface but strange why it is showing error in eclipse.
public class Test
{
default void test() { //Default methods are allowed only in interfaces.
}
}
this one is working fine
public class Test
{
void test()
{
}
}
any reason for it?
The default keyword has nothing to do with default 'package private' access that derives from not specifying the access. The default keyword only applies to interfaces (and not just to functional interfaces), to supply a default implementation in the interface. This is a feature introduced in Java 8 to allow for easier interface evolution.
Overloading the default keyword to mean 'package private' access when used in classes would only be confusing and serve no real value, as the same is achieved by not specifying access.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In our company, we have a rule which says that all files must have a 8 letters prefix in their names.
For instance, Blarghhh_MyFile.java.
Because of Java's limitation that a public class must have the same name of its file, our classes have stupid names. For instance:
Blarghhh_MyClass myObject = new Blarghhh_MyClass();
Instead
MyClass myObject = new MyClass();
Does someone know a workaround to solve that?
You cannot have an alias for classes in Java. But you could simple create a new class and let it extend your old. Something like:
MyLongNameClass.java
public class MyLongNameClass{
//stuff
}
WorkingClass.java
public class WorkingClass {
private void coolFunc(){
MyClass mc = new MyClass();
}
private class MyClass extends MyLongNameClass{}
}
But i would rather type the whole name then do it that way.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The compiler is not accepting the constructor as a constructor but as a method and giving the following remark:
My piece of code:
class Rsesults extends Mark implements Sports {
Results(int x,int y) {
super(x,y);
}
The error:
invalid method declaration; return type required
Results(int x, int y)
Your class and your constructor have different names : Rsesults - Results
Which means that Java doesn't recognize Results as constructor, but thinks that it's a general method ( which means it has to have a return type )
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why eclipse throws an error message while giving public modifier to interface
publid interface one // throws an error
{
}
Public type one must be defined in its own file error i am getting
please give your clarifications regarding this
Create a new file called 'one.java'. Place the declaration of your interface in there.
Every public class, interface etc. needs to be in its own file.
Because your Java file containing one most probably is not named "one.java".
Any public class or interface must be declared in a separate file, having the name of that interface or class.
If you have multiple top level classes/interfaces in the same file, only one of them can be public.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a class. Instance of this class cannot be made. How can I achieve that?
Declare it abstract and add a private constructor.
Do you mean no instances can ever be made? or just one instance?
If no instances can ever be made, then make the class final with a private constructor. All methods then need to be static. A good example of this is java.lang.Math
If you need only a single instance and want no one else to make new instances, then consider using an enum as described in Effective Java 2nd ed:
public enum MyClass{
INSTANCE;
...methods
}
Code that uses this class then invokes methods like this: MyClass.INSTANCE.foo().
If you don't want to instantiate that class then I assume that you only want it to have some static methods. In that case you can easily do it with enum with no constants like
enum MyUtilities{
;//if you want you can place instances of that class here, or not place any
//your methods
public static void myMethod(){
System.out.println("hello");
}
}