• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Style Guide question

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
The last point of the style guide looks like this:
.....................
5.4 - Scope
All class attributes must always be private, except for inner classes.
.....................
Does this mean all methods we create should always be private?
Thanks,
Pauline
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the style guide is refering to class variables being private. From what I've read, class attributes are represented by variables and class behavior is represented by methods. Class attributes should not be directly accessible from outside the class. If access from outside the class is required, it should be done through a method in the class. For example:

The class attribute for a Car, gallonsOfGasAvailable, may only be used from within the class. If another class, such as CarDriver, needs to see or change that attribute then they use the methods to do so. I hope this helps.
Matthew Phillips
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, everything should be as private as possible. That includes classes, methods, and variables.

If you can get away with making all your methods, etc. private, do it. You'll find that you can't make them all private or nothing will run, but play around with it. Usually you can privatize stuff a lot more than you think at first.

 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you read about encapsulation from one or more sources beyond a general Java learning guide. I've found the entry-level books do not stress this fundamental rule as strongly as Marilyn does in her post.
The books I have read that were beyond entry level Java or were OOP related strongly encourage encapsulation.
Michael
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what where these books called if I may ask Micheal
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the example,Mathew. And for defining what attributes are - I wondered what was meant there.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
attributes, data, fields, variables generally refer to the same things as long as you exclude local variables.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the exception for local variables? uunhh, wait a minute, first questions first: what do you mean by local varialbes?
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm not mistaken a local variable is a variable that is declared in a block. between {...} of code.
A variable declared in a method is only visible within that method, but if that varable is also a local variable ?. I'll have to hit the books.
Sorry your previous bartender knew Much, Much ... repeat this about a 1000x times ... ,Much more about Java than I do.

[This message has been edited by Johannes de Jong (edited May 22, 2001).]
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a variable is declared within a method wouldn't it be declared within a block of code anyway? So by that definition it would be a local variable already?
cheers, Pauline
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the terms global and local aren't as applicable to Java as they might be in other 3GL languages, like C.
When I�m programming in Java I think of variable scope differently than when I�m programming in C. A variable's scope is local if it can not be accessed outside of the block of code (class, method, or code block) it was declared in.
Java's private, public, and default scope definitions make this easier to understand. Methods are used to control an object's behavior or cause a change to a characteristic (characteristic = variable).
Basically, I think the issue with variable scope gets more important when we start implementing classes as stand-alone objects. It�s up to the programmer to define how a class will be interfaced with via methods. If variables are accessible outside of the class they might be changed in ways not considered by the programmer. When methods are used to interface with a class it separates code implementation from how the class is interfaced with. Even if class source code needs to be changed its interfaces (constructors and methods) to the outside world can stay the same. That�s a powerful feature of Java.
Michael
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables declared in the class and outside of a method may be either instance variables or (if they are modified with the keyword 'static') class variables.

Methods declared inside methods are local variables.

Public, private, and protected can only modify class or instance variables (and methods, but we're just talking about variables here). If the default (none of the above) is used, the variable has default (sometimes called 'friendly' or 'package') scope. These words do not do much within the same class, but determine the visibility of the variable by other classes.
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for correcting me where I overstepped my explanation Marilyn. It sounded right when I wrote it, but I see now that some of what I wrote was misstated.
Michael
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which Java books do you have on your booshelf, Marilyn ?.
I looked in Just Java & Java 1.2 Unleashed (borrowed it from work, bad book ) and I could not find anything in both on local variables. Know I know youdid not need a book, but still which books do you use
or is that to personal information
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks folks, I think I have to digest all that info for a few days (4 day weekend, what a deal).
Hey Michael, you still haven't revealed the name of your favorite-OOP-beyond-entry-level-Java books...
Pauline
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Which Java books do you have on your booshelf, Marilyn ?"

Teach Yourself Java 1.1 (Lemay & Perkins)
Java How to Program (Deitel & Deitel)
Java in a Nutshell (Flanagan)
Thinking in Java (Eckel)
Just Java 2 (van der Linden)
Exam Prep Java 2 (Brogdon)
Complete Java2 Certification Study Guide (Roberts, Heller, and Ernest)
and a few I haven't read yet.
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the order of preference ?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Some are better on some areas and others are better on other areas.
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which do you think covered OO best ? & a big favour can you put smilies next to them.
if you liked it if you were not too that happy with it.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic