can we declare the variables globally?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can we declare the variables globally??

29th Sep 2016, 2:20 AM
Geo Jose
4 Answers
+ 1
yes you can declare a variable as global. -> if you want to use your variable inside class then declare it as instance variable(class level). -> if you want to use your variable outside class and within same package declare as protected or public and class as protected or public. -> if you want to use your variable outside class as well as outside package then you must declare your variable as well as your class as public. hope you got some clarity...
1st Oct 2016, 6:34 PM
Vaibhav Kharche
Vaibhav Kharche - avatar
0
Declare it public
29th Sep 2016, 2:38 AM
Denis Protasov
0
private static int x; // the variable of your choice must be placed where 'x' is located in this example and the type is where ive put 'int' // then declare the following opening line of your array public static void main(String[] args) { // Hope this helps
1st Oct 2016, 2:16 AM
Eric Kershner
Eric Kershner - avatar
0
yes you can
1st Oct 2016, 7:08 AM
balram sao
balram sao - avatar