如何访问变量的原始值 [英] how to access original value of the variable

查看:123
本文介绍了如何访问变量的原始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这段代码

public static long number;
public static void main(String args[]) {
    String str1 = args[0];
    System.out.println("str1 is = " + str1);
    long number = Long.parseLong(str1);
    System.out.println("number value is = " + number);
}

public static void test1(){
    System.out.println("number value inside test1 is = " + number);
}

假设我将12345作为参数传递,所以我的o / p将是

Say I pass 12345 as argument, so my o/p will be

 str1 is  12345
 number value is  12345
 number value inside test1 is  0

我想要的是能够在test1方法中访问arg [0](用户参数)或数字(12345)的原始值同样。请帮我做。谢谢。!

What I want is to be able to access arg[0] (user argument) or original value of number (12345) inside test1 method as well. Pls help me do it. Thanks.!

推荐答案

将您的班级编号设置为已解析值 str1 。目前,您设置的本地字段没有超出main方法的范围。

Set your class-level number to the parsed value of str1. Currently, you set a local field which has no scope beyond the main method.

使用:

number = Long.parseLong(str1);

而不是:

long number = Long.parseLong(str1);

这篇关于如何访问变量的原始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆