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??
4 RĂ©ponses
0
Kindly share your code so that someone help you accordingly.
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;
}
0
when i call vairable color from another class itâs display as null
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 );
}