Should we declare a public constructor when the class is declared as package private?
I think in this case there is no need to declare a public constructor since the class is not accessible outside the package anyway. But is there some hidden impact when the class has only package private constructor?
Asked by: Leonardo636 | Posted: 21-01-2022
Answer 1
No, you don't have to declare the public constructor; package private constructors will be just as usable. Classes outside the package wouldn't be able to use the constructor anyway, since they can't see the class.
Answered by: Adrian641 | Posted: 22-02-2022Answer 2
If your class is package private then the access levels indicated by the modifier keyword public
together with the default package private access level of the constructor are equivalent.
You can however indicate the behavior you intent the method to have in case the class visibility is changed during development. This may happen when you open some APIs which were previously internal. In that case it looks more conservative to declare the constructor as package private since you do not open all doors at the same time.
Answered by: Stella282 | Posted: 22-02-2022Similar questions
java - Should the constructor of a private inner class be declared public or private?
Is there any actual difference between this
public class OuterClass {
private class InnerClass {
public InnerClass() {}
}
}
and this?
public class OuterClass {
private class InnerClass {
private InnerClass() {}
}
}
java - Constructor cannot be applied to given types, and name should be declared
This is an myprogramminglab question for school that I just can't get to work...
It has to go on one page, is why I have it all together.
My compile error:
Driver.java:3: error: class TestScores is public, should be declared in a file named TestScores.java
public class TestScores
^
Driver.java:51: error: constructor TestScores in class TestScores cannot be applied to given ty...
java - 3D objects only visible when declared in the constructor
I have an app for Android that displays a 3D cube on screen using OpenGL ES. I want the user to click on the screen and send a cube towards that point, originating in the main cube.
I handle the touch input like this:
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
// Fetch the normalized coordinates from the cube
...
java - long declared in class but constructor seems to wait for int
I have the following class in Java:
class Adresse4 {
private String strasse;
private String hausnummer;
private int postleitzahl;
private String ort;
private long telefon;
Adresse4(String strasse, String hausnummer, int postleitzahl, String ort, long telefon) {
this.strasse = strasse;
this.hausnummer = hausnummer;
this.postleitzahl = postleitzahl;
thi...
java - Is it good to Have instance variable assigned to some value instead declared in constructor
Closed. This question is opinion-based. It is not c...
constructor - "Cannot find symbol: Method" but method is defined and declared in Storm.java
In my driver program methods: setDuration, setWind, setPressure, NewStorm, getCategory cannot not be found although they are clearly declared in my Storm.java file. I can't refer to any of them.
import java.io.*;
import java.util.Scanner;
public class StormChaser {
public static void main(String[] args)
{
// Constants
final int MAX_STORMS = 200;
Storm[] List = new Storm[MAX_STORMS]; // array of Storms
...
java - Why might one also use a blank constructor?
I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example:
public class Genotype {
private boolean bits[];
private int rating;
private int length;
private Random random;
public Genotype() { // <= THIS is the bandit, this one right here
random ...
java - What is the preferred Throwable to use in a private utility class constructor?
Effective Java (Second Edition), Item 4, discusses using private constructors to enforce noninstantiability. Here's the code sample from the book:
public final class UtilityClass {
private UtilityClass() {
throw new AssertionError();
}
}
However, AssertionError doesn't seem like the right ...
java - using constructor from the super class
Java does not allow multiple inheritance, meaning that a class cannot inherit from two classes, which does not have anything in common, meaning that they are not on the same inheritance path. However, a class can inherit from more classes, if these classes are super classes of the direct super class of the class. But the class inherits from these classes indirectly, meaning that it does not "see" anything from these upper ...
java - Why can't enum's constructor access static fields?
Why can't enum's constructor access static fields and methods? This is perfectly valid with a class, but is not allowed with an enum.
What I'm trying to do is store my enum instances in a static Map. Consider this example code which allows lookup by abbreivation:
public enum Day {
Sunday("Sun"), Monday("Mon"), Tuesday("Tue"), Wednesday("Wed"), Thursday("Thu"), Friday("Fri"), Saturday("Sat");
...
java - Calling virtual method in base class constructor
I know that calling a virtual method from a base class constructor can be dangerous since the child class might not be in a valid state. (at least in C#)
My question is what if the virtual method is the one who initializes the state of the object ? Is it good practice or should it be a two step process, first to create the object and then to load the state ?
First option: (using the constructor to initializ...
In java, how do I make a class with a private constructor whose superclass also has a private constructor?
As an example:
public class Foo {
private Foo() {}
}
public class Bar extends Foo {
private Bar() {}
static public doSomething() {
}
}
That's a compilation error right there. A class needs to, at least, implicitly call its superclass's default constructor, which in this case is isn't visible in Foo.
Can I call Object's constructor from Bar
Is Object constructor called when creating an array in Java?
In Java, an array IS AN Object. My question is... is an Object constructor called when new arrays is being created? We would like to use this fact to instrument Object constructor with some extra bytecode which checks length of array being constructed. Would that work?
java - Spring constructor params?
Some of my classes have final fields that are populated from the constructor as well as properties that can be assigned from getters and setters.
If I can do this using spring, what does the springcontext.xml file look like that creates objects in this way?
Thank you
syntax - Why must delegation to a different constructor happen first in a Java constructor?
In a constructor in Java, if you want to call another constructor (or a super constructor), it has to be the first line in the constructor. I assume this is because you shouldn't be allowed to modify any instance variables before the other constructor runs. But why can't you have statements before the constructor delegation, in order to compute the complex value to the other function? I can't think of any good reason, a...
java - How much code should one put in a constructor?
I was thinking how much code one should put in constructors in Java? I mean, very often you make helper methods, which you invoke in a constructor, but sometimes there are some longer initialization things, for example for a program, which reads from a file, or user interfaces, or other programs, in which you don't initialize only the instance variables, in which the constructor may get longer (if you don't use helper meth...
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)