Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of names in Java

Tags:

java

A friend told me that this a good example for learning Java scopes, but I don't understand it.

What is a? I am completely lost!

public class scopesexample {

    public static a a = new a<a>(a(new a()));

    public static class a<a> {
        a a;

        public a() {
            this.a = a(a(null));
        }

        public a(a a) {
            this.a = a;
        }

        public a a(a a) {
            return a;
        }

        public String toString() {
            return "a";
        }
    }

    public static a a(a a) {
        return new a<a>(a);
    }

    public static void main(String[] args) {
        System.out.println( a );
        System.out.println( a( a ) );
        System.out.println( a.a );
        a<a> a = new a<a>(a(new a()));
        System.out.println(a.a( a ));
        System.out.println( a );
        System.out.println( a );
        System.out.println(a.class);
        System.out.println(a.getClass());
        System.out.println(a.a);
        System.out.println(a( a ));
        System.out.println(a( a ).a);
    }
}
like image 911
Alice Beatsy Avatar asked Feb 13 '26 23:02

Alice Beatsy


1 Answers

My advice would be to ignore your friend.

He or she is either winding you up, or he or she has no idea about how students learn about scopes.

For a start, by using a as both a class, method and variable name, he has violated code style guidelines, and common sense.

The second problem is that he has added the problem of understanding namespaces to the problem of understanding scopes, which is unnecessary and confusing.

like image 148
Stephen C Avatar answered Feb 15 '26 12:02

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!