Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of @SuppressWarnings("hiding") in eclipse?

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?

like image 243
theapache64 Avatar asked Jul 25 '15 07:07

theapache64


People also ask

What does @SuppressWarnings resource do?

@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.

What is the use of @SuppressWarnings deprecation?

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" ).

What does @SuppressWarnings serial mean?

The line @SuppressWarnings(\"serial\") tells Java not to remind you that you've omitted something called a serialVersionUID field.

What does @SuppressWarnings unchecked do?

Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.

What is @SuppressWarnings static access?

static-access suppresses the compiler warnings related to incorrect static access.

Should we use SuppressWarnings?

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.


1 Answers

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".

like image 95
DamienBAus Avatar answered Nov 11 '22 05:11

DamienBAus