aspose file tools*
The moose likes Cattle Drive and the fly likes Style Guide question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Style Guide question" Watch "Style Guide question" New topic
Author

Style Guide question

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
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

Matthew Phillips
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
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
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
And what where these books called if I may ask Micheal
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Thanks for the example,Mathew. And for defining what attributes are - I wondered what was meant there.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
attributes, data, fields, variables generally refer to the same things as long as you exclude local variables.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Why the exception for local variables? uunhh, wait a minute, first questions first: what do you mean by local varialbes?
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
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

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Mar 11, 2001
Posts: 351
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

Joined: Jul 22, 2000
Posts: 9047
    
  10
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

Joined: Mar 11, 2001
Posts: 351
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
Bartender

Joined: Jan 27, 2001
Posts: 5089
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

Joined: Jan 19, 2001
Posts: 4012
    
    6
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

Joined: Jul 22, 2000
Posts: 9047
    
  10
"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
Bartender

Joined: Jan 27, 2001
Posts: 5089
In the order of preference ?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
No. Some are better on some areas and others are better on other areas.
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Style Guide question