简体   繁体   中英

Java can't access static public method

I did not find a similar question, this is the ref I noticed but did not help: Accessing public static java method from scala

I am confused as to why I can not access the cellPhone method addContact from the Start class? addContact is public and static. If you look at the joseph class I wanted to see the difference between array of object vs ArrayList of objects in terms of access.

I know this is perfectly organized, perhaps I should have the cellPhone class in the Joseph class? But that did not work either.

My Error is in the Start class.

Start class:

public class Start 
{

public static void main(String[] args) 
{
    // TODO Auto-generated method stub
    Joseph jhr = new Joseph();



    jhr.addCreditCard("Visa");

    jhr.setWeight(168);
    jhr.setHairColor("Brown");
    jhr.setGender("male");
    jhr.setName("Randy ");
    jhr.myCellPhone.addContact();//ERROR: he method addContact() is undefined for the type List<cellPhone>

    jhr.cell[0].setCellPhone(5255857);
    jhr.cell[1].setCellPhone(4155053);
    jhr.cell[0].addContact("Bob");
    jhr.cell[1].addContact("Amy");
    //jhr.cell.addContact("Nameishi");
    //jhr.cell.setCellPhone(3333847);
    System.out.println("Single : "+jhr.showStatus() + " Gender: " + jhr.showGender() +" Name:"+jhr.showName());
    //System.out.println("Cell number: " +jhr.cell.showCellNumber());
    System.out.println("Middle name: " + jhr.middleName);
}

} 

CellPhone class:

public class cellPhone {
private int cellPhoneNumber;
static private List<String>  myContacts =  new ArrayList<String>(100);

public cellPhone() {
    // TODO Auto-generated constructor stub
}
//show all numbers in cell phone
public final int showCellNumber() {
    return cellPhoneNumber;
}
//get all Contacts in cell Phone
public List<String> contactsList() 
{
    return myContacts;
}
//add numbers to cell phone
public void setCellPhone(int myNumber) {
    cellPhoneNumber = myNumber;
}
//add contacts to cell phone
static public void addContact(String contact) {
    myContacts.add(contact);
}


}

Joseph Class:

 public class Joseph extends human
{

//public static final cellPhone cell = null;
public cellPhone [] cell = new cellPhone[2];
static public List<cellPhone>  myCellPhone =  new ArrayList<cellPhone>(100);
public String middleName;
private int weight;

public Joseph() 
{
    middleName = "John";
    weight = 0;

    cell[0]= new cellPhone();
    cell[1]= new cellPhone();
    //cell.setCellPhone(3253847);
}

public void setWeight(int setw) 
{
    weight = setw;
}

public int getWeight() 
{
    return weight;
}
}

Also, addContact() is a static method. That means the method belongs to the Class instead of the instance of the class. In other words, all CellPhone instances will share the list static private List<String> myContacts . Remove the static before the method and before the list and it will all make sense.

I have to add a CellPhone to the list, then I have to set it. I think Arrays are easier but obviously Arraylist are dynamic. For anyone with my issue here is how I solved it.

    jhr.myCellPhone.add(new cellPhone());
    jhr.myCellPhone.get(0).addContact("Joseph");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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