Regarding a Class Vs Enums [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I was having a query , I have declared a class in which all the methods are static and it is following the utility design pattern that is it is acting like helper class Now can I replcae that class with correspond to enum also , Can I have enum having all the staic methods inside it, if Yes then what other advantages it offers ..!!

The problem with static method is: they can't be mocked for testing. At least not easily.
Putting the methods in an Enum with a single instance gets you a little closer. I'm not sure if enums can be mocked with the standard libraries, you certainly can't without using reflection.
But if you put your methods in an interface implemented by the enum, and everybody else just using the interface, accepting an instance of that interface via constructor (or setter if you have to) you can mock it as easily as you want.

Yes, you can use an enum as a utility class. There aren't many advantages to it, however: it boils down to the private constructor, which prevents uncontrolled instantiation. I would prefer sticking to the ordinary class with a private constructor since there's an expectation for an enum to be used for an enumerated type and not as a utility class. If you used enum for a singleton, that would give it only a slight bit more sense.

Related

Static methods or empty constructor [closed]

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.

Issues related to declaration of private constructor in a class [closed]

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
I'm new to Java.
I was reading about Encapsulation concept in Object Oriented Programming.
While reading, I saw a line telling that :
If constructor of a class is declared as private, it will cause some
problems.
But it didn't tell what kind of problems could occur.
Can anyone tell what kind of problems are there with private constructor ?
When you declare a constructor of a class as 'private', you will not be able to create new instances "objects" of that class.
This is a problem if you do want to create new instances of the class, but this implementation is useful when making a Singleton Design Pattern.
If you are interested in knowing more about design patterns, I can share some resources with you.
Here is a book by Carlos E. Otero that covers the topic of design patterns:
https://books.google.com.tr/books?id=IL6FBLJn69UC&printsec=frontcover&redir_esc=y#v=onepage&q&f=false
Simple words, object or instance can't be created for that class.
As soon as, you write this statement below. For example
SomeClass c = new SomeClass(); // this will give an exception.
Basically, private constructors are used for making singleton classes.
On declaring a constructor as private you can't instantiate the class using default constructor. So, you need to create a constructor with public to access it outside.

Convert object from one class to another in java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have 2 custom classes A and B.
Now I have an object of A let say 'a' and have to convert it to B class.
Now I have 2 approach
First is I write a transform Util which has a static method for conversion.
The second approach is to write that logic in class A with a method convertToB()
Which one is more accurate. Please suggest.
In short, it depends on the relationship between A and B.
If B extends A the conversion is done within the language when assigning an A object to a B object.
Otherwise, if there is no inheritance at play, it would be advised to either create a util class to do the conversion for the case of future changes, or have a constructor of B that has an A object as a parameter.
This is a matter of preference and a question perhaps better suited for the software engineering/design counterpart to Stack Overflow. That said, I would use the first option. If you ever add more classes and need to convert between them, you will bloat your classes with all the conversion methods. If you have one or more classes with the conversion methods, the code is cleaner.
Separation of Concerns - util class is the best approach

Is it best to call a method (from class A) in a class (class B) which then calls a method in a class (C) or to use a getter for Class C in Class B? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
E.g.
In Class A:
b.doThing();
In Class B:
doThing() {
c.doThing();
}
OR
In class A:
b.getClassC().doThing();
What is the convention for a situation like this?
According to the Law of Demeter, you should go the first way, i.e. only call the method of class B which itself delegates to class C. This way, you reduce dependencies between your classes which is basically a good thing for reusability and maintainability.
Usually it will be the second case unless the implementing class has do to more than executing one line for example setting/checking states or preparing other methods etc.
The big advantage of this is that you don't need to worry about implementation details by calling
b.getClassC().doThing();
You don't care if doThing will change structurally or methodically in which you would supply more parameters or else.
Object orientation is about getting objects to do things for you. Hence you're better off asking 'b' to do something, and the fact that it then calls 'c' is hidden from you, and it can be changed in the future trivially. You don't want to ask 'b' for its component objects and then do something with those.

hiding a static method vs overriding a non-static method [closed]

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 6 years ago.
Improve this question
Using static methods does not take advantage of OOP, so methods are rather overridden, not hidden. Yet hiding of static methods exists in Java. Is it there only to help out in some hopeless cases?
What are the consequences of hiding a static method vs overriding a non-static method? Are there any cases when hiding should be preferred over overriding (up to the point to make a method static, which is usually decided on other merits)?
Java is an object-oriented programming language such that you should always prefer an OO approach from a pure procedural approach, in other words you should always prefer to override a method rather than hiding a static method. I would even say that hiding a static method is a terrible approach because it is very error prone, you should alway prefix a call to a static method with the name of the corresponding class

Categories

Resources