Call vairables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Call vairables

I want to call variable from another class that changed inside one of the method but when I call the variable it’s display =0 or for String =null what it’s not change??

9th Apr 2020, 4:53 PM
Reema
4 Answers
0
Kindly share your code so that someone help you accordingly.
9th Apr 2020, 4:56 PM
Muhammad Bilal
Muhammad Bilal - avatar
0
Public class FlowersColors { Scanner input1 =new Scanner(‪System.in‬); String color; int numberOfcolor; public String a2 (int total) { try{ System.out.println("We hve 2 colors of the Gypsophila " ); System.out.println("(1)- Blue"); System.out.println("(2)- Yallow"); System.out.println(">Please enter the number of color you want of the flowers;"); numberOfcolor=input1.nextInt(); while(numberOfcolor!=1 & numberOfcolor!=2 ) { System.out.println("Please enter number of these color that you want Enter again: "); numberOfcolor=input1.nextInt(); } } catch (InputMismatchException e) { System.out.println("Error you did't enter a number"); } if (numberOfcolor==1) { color="Blue"; } if (numberOfcolor==2) { color="Yallow"; } return color; }
9th Apr 2020, 5:31 PM
Reema
0
when i call vairable color from another class it’s display as null
9th Apr 2020, 5:35 PM
Reema
0
if you do something like: var p = new FlowersColors() .a2(0); System.out.println(p.color ); you can get error: cannot find symbol: variable color because a2() returns String and then p is String and it has not color //try this public static void main(String[] args){ var p = new FlowersColors(); p.a2(0); System.out.println(p.color ); System.out.println(p.numberOfcolor ); }
9th Apr 2020, 9:39 PM
zemiak