Skip to main content

All Questions

0 votes
2 answers
241 views

Constructor Chaining and Visibility in Java

Consider this class: public class Passenger { public Passenger() { } public Passenger(int freeBags) { this(freeBags > 1 ? 25.0d : 50.0d); this.freeBags = freeBags; } ...
Eug's user avatar
  • 185
107 votes
4 answers
74k views

Private constructor in Kotlin

In Java it's possible to hide a class' main constructor by making it private and then accessing it via a public static method inside that class: public final class Foo { /* Public static method */...
Marvin's user avatar
  • 1,900
-2 votes
1 answer
58 views

In Java, what do you call a class with a private constructor or many private constructors? [closed]

What would it mean if a class had only private constructors? Can you name such a class?
Lorren112's user avatar
-2 votes
2 answers
173 views

How to set a GUI non-visible within the constructor?

I have a swing GUI class Foo and in the constructor of Foo class, I load some values from the databases. Based on the returning values from database, I can say whether this GUI should be visible or ...
drJava's user avatar
  • 777
7 votes
3 answers
2k views

Java, compilation error, Constructors

I have been trying a mock ocjp 6 test. I went though a question asking if the constructor is correct : 1- public Test8(){} 2- private void Test8(){} 3- protected Test8(int k){} 4- Test8(){} The ...
oueslatibilel's user avatar
0 votes
2 answers
57 views

Is there an elegant way to change what methods/variables an object of a class can use based on the constructor used to create this object?

So for example, when the object is constructed with lets say a constructor without arguments, this object is able/"allowed" to use a certain subset of the methods defined in the class, and when the ...
Ceiling Gecko's user avatar
1 vote
1 answer
132 views

Java UNIXProcess not visible in Eclipse?

I tried to use the UNIXProcess.class, but the eclipse keeps warning me that UNIXProcess is not visible. The code is simple: import java.lang.Process; public class Simple { UNIXProcess process; ...
wlhee's user avatar
  • 2,514
22 votes
7 answers
37k views

Aren't Java constructors public by default? [duplicate]

I have two classes in two different packages. For one class I've defined a constructor without setting access modifier for it. I want to instantiate an object of this class in another package and get ...
Eugene's user avatar
  • 59.9k
3 votes
4 answers
122 views

Why my object sees variables which were not given to it in the constructor?

I have the following code. Which is "correct" and which I do not understand: private static void updateGUI(final int i, final JLabel label) { SwingUtilities.invokeLater( new Runnable() { ...
Roman's user avatar
  • 129k