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
I got 3 classes, A,B and C.
Only 1 instance of class A exits at any given time but hundreds of objects of class B and C will exist.
Class A calls a method of class B.
Class B calls a method of class C.
And class C eventually calls a method of the class A object.
What would be the best and cleanest way to provide the reference to the class A object in C?
Saving it in a private variable and initializing it via the constructor?
Creating a static getInstance() method in Class A which returns the object itself?
Or passing "this" through parameters from A to B to C?
Any C Object will call the method of A multiple time in his lifetime.
The most convenient way is certainly #2. The "cleanest" is probably #1, or #3, depending on the circumstances.
The Singleton (#2) pattern has some problems. That does not mean you should avoid it at all costs. But you should be aware of the implications.
For some information on the topic see What is so bad about singletons?
I would suggest create a static getInstance() method in Class A which returns the object itself.
Option 2. Have your Singleton class contain the instance privately with a private constructor and return the instance using get instance. This the commonly used pattern.
Option 1 makes the constructor open to other methods which is not needed since it should only be called once (if I'm reading the option correctly). Option 3 will clutter your code.
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 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.
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.
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 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.