Alice Beatsy
Alice Beatsy

Reputation: 23

Scope of names in 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);
    }
}

Upvotes: 2

Views: 142

Answers (1)

Stephen C
Stephen C

Reputation: 718778

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.

Upvotes: 10

Related Questions