This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Constructor with a parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor with a parameter" Watch "Constructor with a parameter" New topic
Author

Constructor with a parameter

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

This is my code:


public class A {

A(int d) {

System.out.println(d);

}

public static void main (String [] args) {

try {
int h = Integer.parseInt(args [0]);
}

catch (Exception e) {}

A x = new A(h);

}
}


It does not work. The compiler tells: cannot find symbol, symbol : variable h.

But the symbol is there? What is wrong.

Regards
Urs


SCJP 1.4
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
"h" is defined within the try block, inside the { } braces. It goes out of scope at the close brace, so the compiler can't "see" it any more. This is one bit of syntax that annoys me because it forces us to declare the variable before the try block, before we use it. This makes "h" visible to the try block and the catch block:

Now, is that really the logic you want or just an example of the compiler message? This only tries to create x if the argument causes an exception, right?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Surely, the simple solution to getting the scope you want for variable "h" is just to enclose the bit of code that uses "h" within some braces.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Now I have that code:


public class A {


A(int d) {

System.out.println(d);

}

public static void main (String [] args) {

int h;

try {
h = Integer.parseInt(args [0]);
}

catch (Exception e) {}

A x = new A(h);

}
}

Still it does not work. I do not understand the scope of h.
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Originally posted by Urs Waefler:
Now I have that code:





The above code should not give warning concerning h as far as I can tell(and if, it will state that h might not be initialized).

What I would do, if I were you - would be to solve the problem in small steps. For example, instead of initializing h with: Integer.parseInt just do something like: int h=10;

A variable, like h, lives - which means, that it is visible -in a scope. You can see a scope as the space between a { and the end tag }.

In your code, h's scope is the main method.

does this help you?

/Svend Rost
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor with a parameter
 
Similar Threads
Scanner
Beginner Question: Class Capabilities
Dan Mock Exam Question(Hascode)
hash code doubt from dan's ques
User Defined Package Doubts