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.
Keeping Util class methods static is a good practice or instance methods are good?
If a method is an utility method then it has no meaning in being associated to an instance of an object so there is no reason to make it an instance method.
An instance method should be something that is meaningful to a specific instance of a specific class.
Usually, you have an Util class with static methods so that you don't need to create an instance of your Util class.
I don't see the point in having an instance of an Util class, so I'd say keep the methods static.
if your method is not depend on other non static member. your method should be static.
i think here you are making utility pack so. method should static if it is not depend on non static member :)
generally util has no instance of util class
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.
What kind of classes can be extended by interfaces? What are the rules?
All the Java programmers I know think that interfaces cannot extend classes but
java.util.concurrent.ExecutorService:
public interface ExecutorService extends Executor { //...
java.util.concurrent.Executor:
public class Executors { //...
So, it looks like some interfaces can extend some classes. What are the rules? I only noticed that java.util.concurrent.Executor has static members only and a private constructor. Can somebody explain the rules and the purpose? Thanks.
Interface can only extend another interface and not a class.
Since interfaces have no actual implementation of any logic (this is not possible in Java), there is no fear of possible collisions if some interfaces have the same method signatures.
Executor is an interface and not a class, unless Javadoc is mistaken or I misread your post. So there is not any problem with another interface extend it.
Every single method declared in an interface will have to be implemented in the sub-classes.
A class can implement multiple interfaces and it can not be private.
A class that implements an interface must provide an implementation of all the method of that
interface. as far as doing it the other way around as
#darijan says "Interface can only extend another interface and not a class......."
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 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 11 years ago.
I want to know what the HelperClass used for. Can someone please explain it with examples?
Thank You
A helper class (in Java, at least) is usually just a static utility class with a variety of methods. In general, a single helper class deals with related functionality, like Apache Commons' StringUtils class, that has a bunch if static methods that operate on strings.
In Java, these types of classes often exist to wrap up functionality that either cannot (for example, the String class is final, so additional functionality can't be added) or should not be addedto the class itself.
In a nutshell, however, a helper class is a class that helps, by providing general or specific functionality in a generic, reusable, encapsulated way.
It's usually used to preserve the cohesion of a class (in Java). Useful methods that don't really belong in any of the classes context can be put in a helper class (static of course) so you can call them.
They are usually static methods. They dont belong to a class or a context of the class, but instead using a class, you can provide some sort of functionality with a helper class/ static classes.
Informal way of referring to Utility Class
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.