Solved

explanation of Java style issues

Posted on 2002-03-17
5
235 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Impossible to extract MSI from new JAVA releases 2 51
starter POM and spring-boot-starter,  spring-boot-web 2 43
Java JRE greater than 1.6 5 51
print map entry 34 56
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 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

26 Experts available now in Live!

Get 1:1 Help Now