I am new in programming. I was looking at this answer and found a list of possible values for the @SuppressWarnings annotation. but I can't figure out the usage of the value hiding. Can anyone help me with an example?
@SuppressWarnings annotation is one of the three built-in annotations available in JDK and added alongside @Override and @Deprecated in Java 1.5. @SuppressWarnings instruct the compiler to ignore or suppress, specified compiler warning in annotated element and all program elements inside that element.
The @SuppressWarnings annotation disables certain compiler warnings. In this case, the warning about deprecated code ( "deprecation" ) and unused local variables or unused private methods ( "unused" ).
The line @SuppressWarnings(\"serial\") tells Java not to remind you that you've omitted something called a serialVersionUID field.
Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.
static-access suppresses the compiler warnings related to incorrect static access.
Annotation Type SuppressWarnings As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.
From xyzws,
A class can declare a variable with the same name as an inherited variable from its parent class, thus "hiding" or shadowing the inherited version. (This is like overriding, but for variables.)
So hiding basically means that you've created a variable with the same name as a variable from an inherited scope, and the warning is just letting you know that you've done it (in case you needed access to the inherited variable as well as the local variable).
An example is:
public class Base {
public String name = "Base";
public String getName() { return name; }
}
public class Sub extends Base {
public String name = "Sub";
public String getName() { return name; }
}
In this example, Sub
hides the value of name
given by Base
with it's own value - "Sub"
. Eclipse will warn you - just in case you needed the original value of the variable name
- "Base"
.
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