java - How to access non-static variables & methods from a different class -
java - How to access non-static variables & methods from a different class -
i'm not quite understanding utilize of "static" properly.
i have class includes variables want access different class.
however when seek access getter method different class error stating
"non-static method getaccnumber() cannot referenced static context."
so how find variables value without making static. problem if create static every instance of object overwrites previous ones value. end same business relationship number in case.
let me explain in detail...
i have class called account, contains variable called accountnumber. getter method called getaccnumber(). have sec class called accountlist seperate arraylist class store instances of account. want create method remove element based upon it's accountnumber. i'm searching , using getaccnumber() within accountlist class compare user parameter , removing if correct! can't utilize method without making static!! help/explanation appreciated :)
i wanna do..
public boolean removeaccount(string accountnumber) { for(int index = 0; index < accounts.size(); index++) { if (accountnumber.equals(account.getaccnumber())) { accounts.remove(index); homecoming true; } } homecoming false; }
thankyou!
let's take illustration have
public class { static void sayhi() { system.out.println("hi"); //other stuff }
and
public class b { void sayhi() { system.out.println("hi"); //other stuff }
then
public class c { public c() { a.sayhi(); //possible since function static : no instantiation needed. b.sayhi(); //impossible : need instantiate b class first }
java static
Comments
Post a Comment