我正在尝试将ArrayList公开(在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?

查看:111
本文介绍了我正在尝试将ArrayList公开(在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公开(在Java中),以便可以在其他类中访问它,我在做什么错呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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