Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C# i can write a sub like
C#
public void example(Boolean closeControlOnFinish = true){
}

Where closeControlOnFinish is by default true so i can call example(); without having to call example(true);.

How do I replicate this in java? The following doesn't work. Additionally note that the code is in public class Download extends
Java
java.awt.Panel.

public Download(Boolean closeControlOnFinish = true) {
}
Posted
Updated 9-May-12 7:36am
v3

1 solution

There is no exact same feature in Java, and even if C# it was introduced in v.4.

For a reasonable work-around, take a look at this answer:
http://stackoverflow.com/questions/965690/java-optional-parameters[^].

—SA
 
Share this answer
 
Comments
A-s-h-l-e-y 9-May-12 15:32pm    
So is there a way i can use public Download(Boolean closeControlOnFinish) {
}
and still call it by just Download(); ?
then use if null closeControlOnFinish=true.
currently net beans just gives an error preventing this
Sergey Alexandrovich Kryukov 9-May-12 17:18pm    
There is nothing preventing it. You must be doing something else wrong. No, you did not get it. You don't "still call" the same method without parameter; you have to define two (or more) methods with different parameter sets (signatures) but the same name. They are different methods. One can call another one.
--SA
A-s-h-l-e-y 9-May-12 22:30pm    
I see i can switch out using a 3rd sub and a global so i don't require the passing of the variable locally. Thanks :).
Sergey Alexandrovich Kryukov 9-May-12 23:41pm    
You are welcome.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900