Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A A = new A()  RSS feed

 
FY Hsieh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually do
A a = new A();
a.doIt();
..

But it seems
A A = new A();
A.doIt();
..

is perfectly fine. Why neither compiler and run time system complain about the vague statement of "A A = new A()" ??
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by FY Hsieh:
... Why neither compiler and run time system complain about the vague statement of "A A = new A()" ??

I don't think this statement is vague, because 'A' is a perfectly valid identifier and the context makes it clear. However, I also think it's clear that using variable names like this is a Bad Idea in terms of human readability.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works because as marc has already said the compiler can differentiate between wheer it expects to see Class names and variable names. This also applies to method names and attribute names so if you really wanted to be obtuse you could write something like

Although please don't ask me for a job if you plan to write code like this
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is as the staff, spoke, the compiler makes one casting of that class A is different of int A, plus this is not a legibility question, in a development team this can confudir the beginning programmer or until programmers with more experience. This would be a good question for certification.

bye!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!