[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

explanation of Java style issues

    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
swh062500
Asked:
swh062500
1 Solution
 
maggariCommented:
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
 
m_onkey_boyCommented:
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
 
harvkCommented:
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
 
CEHJCommented:
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
 
swh062500Author Commented:
    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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now