简体   繁体   English

Java 中的方法和变量范围问题

[英]Method and Variable Scope Issue in Java

I need help I cannot figure out how to fix the scope of my variables.我需要帮助我无法弄清楚如何修复变量的范围。 I want this to be an example for my notes but have been on it for almost 2 hours.我希望这是我笔记的一个例子,但已经写了将近 2 个小时。

public class methodPractice{
    String streetName;
    int streetNum;
        public static void streetName()
    { 
         String streetName = "Pope Ave.";
    }
        public static void streetNum()
    {
        int streetNum = 11825;
    }
        public static void main(String[] args)
    {
        streetName();
        streetNum();
        System.out.println("This is your home adress: " + streetNum + 
        streetName);
    }
}

Thank you for your help.谢谢您的帮助。

You are shadowing the fields.你在遮蔽田野。 Use this to make sure you get the fields, or a compile error.使用this来确保您获得字段或编译错误。

public static void streetName()
{ 
    this.streetName = "Pope Ave.";
}

public static void streetNum()
{
    this.streetNum = 11825;
}

Here is your main method, with line numbers added:这是您的main方法,添加了行号:

1.    public static void main(String[] args) {
2.       streetName();
3.       streetNum();
4.       System.out.println("This is your home adress: " + streetNum + streetName);
5.    }

A few points...几点...

  • When line 2 runs, "streetName()" calls the static method below.当第 2 行运行时,“streetName()”调用下面的静态方法。 The static keyword says you are free to call the method by itself – that is, you don't need an object; static关键字表示您可以自由调用该方法本身——也就是说,您不需要对象; you don't need to call new methodPractice() first.你不需要先调用new methodPractice()

     public static void streetName() { String streetName = "Pope Ave."; }
  • When line 3 runs, it's the same thing: "streetNum()" calls a different static method – again, totally fine to call this by itself.当第 3 行运行时,它是同一件事:“streetNum()”调用了一个不同的静态方法——同样,完全可以自己调用它。

     public static void streetNum() { int streetNum = 11825; }
  • Line 4 is different, there are a few things going on.第 4 行不同,发生了一些事情。 Your expectation is that "streetNum" finds the int that you declared on the class, but it doesn't work.您的期望是“streetNum”找到您在课堂上声明的int ,但它不起作用。 Why?为什么? Because you defined that member with "int streetNum" – without "static".因为你用“int streetNum”定义了那个成员——没有“static”。 So what?所以呢? Without being declared static, it means "streetNum" belongs to an object instance .没有被声明为静态的,它意味着“streetNum”属于一个对象实例 What does that look like?那看起来像什么? Here's an example showing object creation, followed by setting the object member "streetNum" to 1.这是一个显示对象创建的示例,然后将对象成员“streetNum”设置为 1。

     methodPractice object = new methodPractice(); object.streetNum = 1;

You could work around this by declaring both of the non-static members to be static ( static String streetName , and static int streetNum ).您可以通过将两个非静态成员声明为静态( static String streetNamestatic int streetNum )来解决此问题。 Or you could leave them as is, and interact with them through an object instance (after doing new .. ).或者你可以让它们保持原样,并通过对象实例与它们交互(在执行new ..之后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM