This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Programming style insights  RSS feed

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


"Fields are always public static final. Some programmers tag methods as public, and fields as public static final. Some programmers do that however, the Java Language Specification recommends that the redundant keywords not be supplied, and, we will follow that recommendation."

P. 248 of Core Java Vol I (8th Edition) available here.

Personally, I disagree with the authors, and, would prefer access modifiers to be declared and typed explicitly for greater clarity. Moreover, the use of UPPERCASE
is noted for constant SPEED_LIMIT, whilst the Java Programming Style Guide seems to promote the use of camelCase notation throughout, what do you consider common practice for static finals (i.e. constants) as a Java programmer?

Conversely, outer class methods cannot access fields of the inner class, unless they have been assigned public access, do you find this behaviour misleading and inconsistent?



Output
Error:
C:\Documents and Settings\User\Desktop\JavaDev\InnerClassTest.java:40: cannot find symbol
symbol : variable testValue
location: class TalkingClock
if (testValue) {System.out.println("Works?");}
^
1 error

Tool completed with exit code 1

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16027
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:Personally, I disagree with the authors, and, would prefer access modifiers to be declared and typed explicitly for greater clarity.

Well, that's a matter of personal preference.

Jon Camilleri wrote:Moreover, the use of UPPERCASE is noted for constant SPEED_LIMIT, whilst the Java Programming Style Guide seems to promote the use of camelCase notation throughout, what do you consider common practice for static finals (i.e. constants) as a Java programmer?

All uppercase with words separated by underscores is what seems to be the de-facto standard in Java software for constants (static final fields as well as enum constants).

Jon Camilleri wrote:Conversely, outer class methods cannot access fields of the inner class, unless they have been assigned public access, do you find this behaviour misleading and inconsistent?

The fact that class TalkingClock in line 27 cannot access the member variable testValue of the inner class TimePrinter in your example doesn't have anything to do with testValue being protected instead of public. You have to have an instance of the inner class to be able to access it:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!