Solved

explanation of Java style issues

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 293
web project error add remove 1 49
junit as external jar or library 7 37
batch vs regular insert in spring DAO 2 22
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

756 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