close up door 6 year ago.
You are watching: How to call a variable from another class in java
Within my Key approach I"m trying come understand also just how to call up a variable indigenous a different class.
I"ve attempted come break it down come ns Many easy Solution feasible just so i deserve to obtain mine heADVERTISEMENT approximately the logic involved.
i have two classens within mine packAge "var":
class 1 - Source.java
packAge var;publicly class resource inns source1; course setSource inns source1 = 5;class 2 - Var.java
packPeriod var;publicly class Var public static voi would main(String<> args) int Var; Var = Source.setSource(); first tins write-up below however I"ve spent 4 days and also nearly every mine spto be time trying to figure thins out, please be tenderness I"m dedicated yet incredibly newbie best now. Many thanks in advance, i hope I"ve submitted this correctly.
request Nov 14 "14 at 18:16
5911 yellow badge11 silver badge77 copper badges
add a commenns |
4 Answers 4
active oldest Votes
Okay, i deserve to type that watch what you to be reasoning however you"ve got A few of ns semantics incorrect. Wcap girlfriend desire to define is a method. A technique takes the following structure:
()therefore for example
publicly voidentifier doSomething(String value) // This is The general public approach the retransforms nothing. It is dubbed doSomepoint // Ins expects a string worth that ins will speak to "value"In her case, you want to develop among these, and also friend desire come do a setter and also a getter (or accessor and mutator if you"re gift posh).
Thins is simply a Typical method. Itns purpose ins to collection the value of some class field. Therefore let"s specify our class..
See more: Everything Is Love By The Carters Everything Is Love Zip, The Carters
windy course Myclass exclusive int num;currently we"ve acquired a class Myclass via a field num. However oh no, it"ns private, therefore let"s produce a setter so the the user can upday ns value.. Complying with ours recipe for methods, we begin with a windy access modifier. We climate define the rerotate type, which is voidentifier Because it returns nothing. The name of the approach should follow ns java naming convention, which is The word "set" complied with through ns name that the member and also lastly the worth because that the setter.. Or every together:
publicly voidentifier setNum(inns num) this.num = num;This will upday the worth in the course with the worth that friend happen in. Excellent!
Well, thins ins nice and also simple. Following ours formula, ins is an approach that ins windy Because everyone have the right to access it; it retransforms something (in thins case int) for this reason that ins ns rerotate type; ns surname adheres to ns conventi~ above that "get" followed through the surname and also it expects no parameters.
public int getNum() return num;This will rerevolve ns worth the num.
Finally, utilizing them!
windy course Maincourse windy revolution voi would main(String<> args) Myclass myclass = brand-new MyClass(); // develop a brand-new Mycourse instance. MyClass.setNum(4); // Upday the value in the course through the number 4. System.out.println("ns number is " + myClass.getNum()); // Outputs: "ns number is 4"