Solved

explanation of Java style issues

Posted on 2002-03-17
5
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 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