我正在尝试将 ArrayList 设为 public(在 Java 中),以便我可以在不同的类中访问它,我做错了什么? [英] I'm trying to make an ArrayList public(in java) so I can access it in a different class, what am I doing wrong?

查看:41
本文介绍了我正在尝试将 ArrayList 设为 public(在 Java 中),以便我可以在不同的类中访问它,我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现错误,如果无法访问另一个类中的 ArrayList,我不确定我应该做什么

There is an error and I'm not sure what I'm supposed to do without being unable to access the ArrayList in another class

package LifeGame;
import java.util.ArrayList;
public class Lists {

    public static void main(String[] args) {
    public ArrayList<String> tasks = new ArrayList<String>(); { // error on `tasks`
        tasks.add("Phone");
    }

    }
}

推荐答案

一个方法内的变量只能从同一个方法内通过名称访问.类成员——在方法之外定义的变量——是唯一可以是公共、私有等的成员.

Variables inside a method are only accessible by name from inside that same method. Class members — variable defined outside of methods — are the only ones that can be public, private, etc.

这篇关于我正在尝试将 ArrayList 设为 public(在 Java 中),以便我可以在不同的类中访问它,我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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