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.
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 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 6 years ago.
Improve this question
I went through some code today. And I spotted something like this:
public class SomeClass
{
...
final private class SomeHandler implements Blahblah
{
...
}
}
Since no one should be able to access SomeHandler from outside, I fail to see why we should make it final. Does anybody have some different insight?
Declaring a class as final has two related but distinct purposes.
It tells the compiler "do not allow this class to be extended". This is done primarily to prevent mistakes.
It tells the reader "do not worry that this class might be extended". This is to aid understanding.
In the case of a private final inner class, we can assume that one person writes or modifies the class and that they do it after understanding the design. Thus, the likelihood of someone mistakenly extending a class that shouldn't be extended (by design) is small.
However, someone reading the code who needs to know is the private inner class might have been extended needs to scan the entire Java source file ... unless the class has been declared final.
Hence final could be serving a useful purpose ... in making the design manifest to improve readability.
On the other hand, the final in the example you found could have been added without any particular intent; i.e. it could simply be "redundant".
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.
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 6 years ago.
Improve this question
Is it possible to implement composition in Java?
If we create a class A and use an instance of this class in class B, then how can we ensure that A cannot create an independent object by itself?
I do not think it is possible. The only similar solution is to create A as private inner class of B. Then nobody else than B can instantiate A.
You could have your composed classes as an inner class of your container class.
class ContainerClass{
class ContainedClass1{
}
class ContainedClass2{
}
}
This way they are tied to an instance of ContainerClass and cannot be created without an instance of the same. You can make them private, then they can only be created within the class
If I understand you question correctly, what you want is an inner class. Essentially to create an inner class you write class A inside class B.
You can create class A as static inner class of B and keep the constructor of A as private. Then only B can use this constructor.
Some links for reference:
scope of private constructor in Nested Class
Maybe you can use classLoaders to determine if A class loaded from B or not
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.