File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Method's Parameter Acess from main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Watch "Method New topic
Author

Method's Parameter Acess from main()

Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
How can i access local method's parameters from the main method.
thanks
Rashid
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Hi there!
You can't. The local variables of a method are limited in scope to that method. They do not exist outside the method. You can always return a value of an array of values from a method, but otherwise there is no way of reaching the local values.
If you really need to access the variables you use in a certain method from some other method, you can always declare those method in the class. Now they are visible in every method in the class, eg:
public class A {
public int number = 5;
public static void main(String[] args) {
A a = new A();
a.aMethod();
a.anotherMethod();
}
public void aMethod() {
number += 7;
}
public void anotherMethod() {
System.out.println(number);
}
}
Hope this helps!
/Kaspar
Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
Thanks Kasper very much for your reply.
I applied the same procedure in my program by declaring variable as class variable and apply them for calculation and then access them from the main.
Given the following example:

public class TryMethod
{
public static long a , b ,c ;
public static void tryInt( long i ) // there's a problem 'i'
{
a = i / 100L
.....
}
...main(...)
{
// command line input passing to above
// method tryInt(long i )
String s = args[0] ;
TryMethod app = new TryMethod();
app.tryInt( Long.parseLong( s ) ) ;
print ( a, b, c ) // works fine
// then here i want to print the above method param 'i'
// which is not accessing here as it's in method scope.
}
If there's any suggestion, please let me know.
Thanks again.
Rashid
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Hello!
Why not do like this (your main method):
...main(...)
{
// command line input passing to above
// method tryInt(long i )
String s = args[0] ;
TryMethod app = new TryMethod();
long number = Long.parseLong( s );
app.tryInt( number ) ;
print ( a, b, c ) // works fine
print ( number )
}

Or maybe you could do like this (if you want to be able to reach you variable from a different method:

public class TryMethod
{
public static long a , b ,c ;
public static long number;
public static void tryInt( long i ) // there's a problem 'i'
{
a = i / 100L
.....
}
...main(...)
{
// command line input passing to above
// method tryInt(long i )
String s = args[0] ;
TryMethod app = new TryMethod();
number = Long.parseLong( s );
app.tryInt( number ) ;
print ( a, b, c ) // works fine
print ( number )
}
Hope this helps!
/Kaspar
Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
Thank you very very much Mr Kaspar for your quick and effective replies.
Now with your responses my problem is soved. Thanks again.
kind regards
Rashid
 
GeeCON Prague 2014
 
subject: Method's Parameter Acess from main()