I was browsing the web and found this example. Within the public static void main method is this line syntax which I have never seen before __main:
As in:
public class Klass {
public static void main(String[] args) {
// code goes here...
__main:
// and mode code here...
}
}
I tried punching this into eclipse and got a little yellow underline with a tool-tip which said "The label __main is never explicitly referenced".
I can't find anything online and I'm really curious! I've been programming in Java for years and I've never seen this syntax before. What does it do, why would you use it, where can I learn more?
The double-underscore is typically used within the system to prevent possible collisions with other identifiers... usually it is used in cases where the scope is available to the user.
Core Java bootcamp program with Hands on practice In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name.
A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses. This is also called name mangling—the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.
The use of double underscore ( __ ) in front of a name (specifically a method name) is not a convention; it has a specific meaning to the interpreter. Python mangles these names and it is used to avoid name clashes with names defined by subclasses.
That's a label. You can use it with the break
and continue
statements. It's not necessary, to use the double underscore.
To be more precise: it is (the first) part of a Labeled Statement (JLS 14.7):
Identifier : Statement
Double underscores in Java have not semantic meaning (the same way that single underscores don't have any semantic meaning).
The only meaning they could have would depend on some naming convention that defines what they are used for.
It looks like the code you look at uses double underscores for labels. Since labels are not frequently used in Java, I wouldn't worry to much about it.
A goto statement in C and its counterparts are accompanied by a Label argument. In a defined method, a goto label;
statement will fire the routine proceeding the label. Below is an example demonstrated by Greg Rogers in this Post.
void foo()
{
if (!doA())
goto exit;
if (!doB())
goto cleanupA;
if (!doC())
goto cleanupB;
// everything succeed
return;
cleanupB:
undoB();
cleanupA:
undoA();
exit:
return;
}
A goto
can be a very effective tool in Java, but Java does not explicitly support the goto
keyword even though the keyword has been reserved by the language. Using a break statement will enable the command jump out of the label to proceed to the statement after the label.
Example:
public class Klass {
public static void main(String[] args) {
// code goes here...
__main:
{
if(args.length==0)
{
break __main;
}
}
//code after label
}
}
The package com.sun.org.apache.bcel.internal.generic.GOTO
, I have not used it personally but I think it can aid to achieve the same code structure as demonstrated by Greg Rogers like this:
void foo()
{
if (!doA())
GOTO exit;
if (!doB())
GOTO cleanupA;
if (!doC())
GOTO cleanupB;
// everything succeed
return;
cleanupB:
undoB();
cleanupA:
undoA();
exit:
return;
}
void undoB(){}
void undoA(){}
boolean doC(){return false;}
boolean doA(){return false;}
boolean doB(){return false;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With