java - Access method from another class No static? -


i had lot of problems in code because of static methods , variables. i'm new programming , noticed when use static in class can call wherever want. link below previous question(if need to, why of trying not use static) told me remove static method solved part of problem.

ps: english 3rd language may bad. please me i'm stuck

get random object arraylist specific variable

so problem:

i have constructor create cars , parameter filled functions. since object not yet created can't methods. there way cant call them? here code:
right cars.getposition() underlined red , telling me make static.

from garage class cars created:

public void addcar() {     cars car = new cars(cars.getid(), cars.askcarid(), cars.getposition(), attendant.askforatt(), system.currenttimemillis());     mygarage.add(car);     if(!(car.getassignedto()).equals(null)){         car.getassignedto().setassign(car);         car.getassignedto().setavailable(false);     } } 

from car class:

private static void createcarsid() {      (int x = 0; x < garage.getcarscapacity(); x++) {         string tempcarid = ("cr" + (x + 1));         temparray2.add(tempcarid);     } }  public static string getid() {      createcarsid();     string tempid = null;     string temppos = null;     (int x = 0; x < garage.getcarscapacity(); x++) {         if (temparray2.get(x) != null) {             tempid = temparray2.get(x);             temppos = temparray2.get(x);             temparray2.remove(temparray2.get(x));             getpos(temppos);             //temparray2.get(x) = null;             break;         }     }     return tempid; }  public void getpos(string idtopos) {     string strpos = idtopos.substring(2);     int pos = integer.parseint(strpos);     position = "gr" + pos;  } 

this 1 of ways can it. new car's information taken keyboard, doesn't need that.

garage.java

package yourpackage;  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.util.arraylist; import java.util.scanner; import java.util.logging.level; import java.util.logging.logger;  /**  *  * @author víctor bernabé pérez  */ public class garage {      private final scanner scanint = new scanner(system.in);     private final inputstreamreader instream = new inputstreamreader(system.in);     private final bufferedreader scantext = new bufferedreader(instream);     private final arraylist<car> cars;      public garage() {         cars = new arraylist<>();     }      public void addcar() {         try {             system.out.print("id");             int id = scanint.nextint();             system.out.print("carid");             int carid = scanint.nextint();             system.out.print("position");             int position = scanint.nextint();             long timemillis = system.currenttimemillis();             system.out.print("attendant");             string attendant = new string();             try {                 attendant = scantext.readline();             } catch (ioexception ex) {                 logger.getlogger(garage.class.getname()).log(level.severe, null, ex);             }             boolean free;             string available = scantext.readline();             if (available.equals("s")) {                 free = true;             } else {                 free = false;             }              cars.add(new car(id, carid, position, attendant, timemillis, free));         } catch (ioexception ex) {             logger.getlogger(garage.class.getname()).log(level.severe, null, ex);         }     } } 

car.java

package yourpackage;  /**  *  * @author víctor bernabe pérez  */ public class car {      private int id, carid, position;     private long timemillis;     private string attendant;     private boolean available;      public car(){         //nothing, default constructor     }      public car(int id, int carid, int position, string attendant, long timemillis, boolean available){         this.id = id;         this.carid = carid;         this.position = position;         this.attendant = attendant;         this.timemillis = timemillis;         this.available = available;     }  } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -