Solved

explanation of Java style issues

Posted on 2002-03-17
5
234 Views
Last Modified: 2010-03-31
    I'm new to Java and I'm finding the coding style in both of the books I bought to be a little strange, as compared to VB.  
     I'm hoping for a clear and somewhat detailed reason that   common Java style is to declare variables in the body of the code, rather than in a seprate area at the top of the routine as is the typical VB standard.  
     
     Connected to my frustration with the placement of variables is the lack of a standard nomenclature for scope or type.  I understand the issues with Hungarian notation for Type.  But not telling a reader whether a variable is local to a method/constructor or if it defined at the bottom of the class makes it very unclear where the variable came from and what it might contain when the routine is entered.  

     
 
0
Comment
Question by:swh062500
5 Comments
 

Expert Comment

by:maggari
ID: 6874869
A blind note on variable scope in java:

The life of a variable is limited to the the innermost flower braces {} it is declared in.

There is a little difference with respect to declaration of class level variables and method variables:

Class variables can be declared anywhere with in the braces defined for class. For example:

public YourClass{

int classvar1;

methoddeclaration1(){
int methodvar1;
}


methoddeclaration2(){
int methodvar2;
}

int classvar2;

}

Here classvar1 and classvar2 both have the same scope. classvar1 and classvar2 both can used in methods 1 and 2.

But methodvar1 and methodvar2 have limited scope. They are limited to the methods they are declared in(understandable). And also they can be used only in the statements following their declaration. You cannot have something like this in a method:

public void method1{

methodvar1 = methodvar1 * 2;

int methodvar1 = 0;
}

This is the same in other languages as well.

The only diffence is in the declaration of class level variables. You will get used to this. By the way, welcome to JAVA.
0
 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 75 total points
ID: 6875165
Java programmers usually follow the C++ tradition of declaring a variable just prior th where it is used.  This makes it easier to keep track of how each variable is used, and for how long.
0
 
LVL 1

Expert Comment

by:harvk
ID: 6879375
First of all, take a look at the Javasoft stylistic conventions at:
http://java.sun.com/docs/codeconv/

Secondly, I only have to say that everything is a matter or getting used to it. So, what seems strange now, will become commonplace soon. Since you are new to Java, you will get a clearer view of things as you go learning and coding. Some programmers that used to code in C/C++ follow different style than those who begin with Java. C style is generally different from Javasoft's style (which is my preference as well even though I program for like 15 years).

And finally just a note: You can put curly brackets anywhere you like in the code (i.e. no for/while loop or class definition). The scope of a variable declared inside that block remains there, even if the block has no meaning.

class Foo {
  int a;
  // code
  for (int b=0;;) {
      // code
  }
  if (true) {
      // code
      int verylocal;
  }
  // there is no verylocal here...
  {
      // just a block
      int xyz = 123;
  }
  // no xyz here
}


regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6879845
A cheap little pocket book on Java style that I found very useful is 'The Elements of Java Style'
http://www.amazon.co.uk/exec/obidos/ASIN/0521777682/proteanit-20
0
 

Author Comment

by:swh062500
ID: 6895407
    Syntactically, I get {braces} for scope, though, in practice, my gut feeling is that if you need to separate a block of code that formally, it should probably be its own method or function.  
     
     In practice, is it ever acceptable to declare a variable after it is used (e.g., at the end of the routine)?  The sun.com link explicitly says not to, but I've seen it done in examples in books.  That makes determining the scope of a variable in a method tricky, b/c the reader might get to the method call prior to part of the outer code that delares a instance variable used in the method.  

     75 points to the first person to validate my complaints about the book I bought... ;-]
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now