It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a funny question on the way home
I have an Enum
public enum Gender{
Yes(Constants.male()), female(Constants.female());
private final String value;
private Gender(String option){
value = option;
}
}
.. should I encapsulate value or just declare it as public?
Is there a disadvantage of run-time initializing the value?
In your case there shouldn't be much difference, the String class is immutable. It is recommended however as encapsulation is good practice.
As far as "run-time" init, I am not sure what you mean. Enum's as essentially singleton's so this would be inited when it is class-loaded. This is at runtime, yes, but there are no disadvantages; especially if you do not want to hardcode the values.
EDIT
As #GyroGearless points out the field should be declared as final, this is best practice even if it isn't public as it's a constant set in the constructor.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Can any one explain me the actual concept of Creating and Initialising an object in java?why a default constructor is used in initialising an object?
What is the need of a default constructor in initialising an object of a class??
Can any one explain me the actual concept of Creating and Initialising an object in java?
It is not clear what you don't understand, perhaps you imagine there is more going on than there is.
Creating - creates an object
Initializing - gives the fields values.
why a default constructor is used in initialising an object?
A default constructor is required to ensure all objects have a constructor even if you haven't written one. Without this you couldn't create the object.
What is the need of a default constructor in initialising an object of a class??
To ensure you create objects without having to write a trivial constructor (assuming this is fine)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
ClassLoader is an abstract class, even though it doesn't have any abstract methods. Why is that so? Are there any other abstract classes without abstract methods?
A class with abstract methods has to be abstract. However, an abstract class doesn't have to have abstract methods.
Generally, a class should be abstract whenever it doesn't make sense conceptually for instances of that class to exist. Having incomplete implementation is one example of this (and it happens to be enforced by the compiler), but it's not the only example.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose there is a class A. Which of the following two access modifiers is a default one for a constructor?
public A()
{
private A()
{
//some code....
}
protected A()
{
//some code....
}
}
It means the exact same thing as modifiers to functions and variables, only now it refers to who can CONSTRUCT an instance of the class.
public - any one can call the constructor from anywhere in the code.
private - Unable to construct from outside the class - typically used to enable control over who gets to instanciate the class with the use of a static member factory method. A good example of an appication found here
protected - Like private but now inheritance is involved - any subclass factory method can be used because now they can call this constructor.
As #dasblinkenlight mentions, if you do not specify any modifier, then they default to being package-private, meaning they are only visible to classes within the package.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
When serializable is a marker interface, how could it make the object persistent?
When the body of the interface is nothing how it it perform some action on the class that implements it?
Using ObjectOutputStream makes the object persistable. This class will only serialise classes marked with this interface to prevent you serialising classes which you didn't intend or cannot be serialised.
Note: Some serialisation libraries do not follow all the rules of Serialzation and can ignore the Serializable interface.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
There is a public final class Objects in Java 7 that extendes the java.lang.Object base class.
Will this by default be the base class like or do we need to call the method on "Objects" class and call the corresponding methods?
No. Nothing changes. The Objects class is simply an utility class. It has only static methods, it's in the util package, and does what ObjectUtils from commons-lang is doing.
Objects is final and it extends Object giving you null safe utilities.