+ 1

Can't we intialize a var that is declared into a particular method when the method is called inside the main method?

class MyClass { static void sayHello() { String name; System.out.println("Hello " + name); } public static void main(String[ ] args) { sayHello(); } } So cant we intialize this string var into main when sayhello() is called into it?

3rd Jan 2023, 2:24 PM
harsh singh
harsh singh - avatar
6 Answers
+ 2
do you mean name = "ABC"; in main method? No. Because you declared it in another method so it's scope(exists) is local to that method only. If you want to use the variable in all methods in same class, deckare it out side a method like : Can't we intialize a var that is declared into a particular method when the method is called inside the main method. class MyClass { static String name = "ABC"; // static var (edit) static void sayHello() { System.out.println("Hello " + name); } public static void main(String[ ] args) { sayHello(); name = "main"; System.out.println("Hello " + name); } }
3rd Jan 2023, 5:19 PM
Jayakrishna 🇼🇳
+ 1
Then how do we pass the values as parameter into the method while using it inside main method as it is also declared inside the scope of that method.... like in this example-: class MyClass { static void sayHello(String name) { System.out.println("Hello " + name); } public static void main(String[ ] args) { sayHello("devis"); } } And one more question is that in your given example the "String name" var is non static and you have used it into the static methods ... how's that possible?...or am i misinterpreting something? Please clarify!
4th Jan 2023, 6:16 AM
harsh singh
harsh singh - avatar
+ 1
Yes. Good catch. That should be static. Sorry i forgot to add it. I just wrote without testing. So must be like : static String name = "ABC"; // otherwise you can't access from static methods.. And example should work. As you passing data and storing it variables. The argument variables also local variables to method in which you declared. So what is your doubt here, is not clear to me.. You are calling static sayHello method with argument passing from main method. No global or non-static variables having...
4th Jan 2023, 8:37 AM
Jayakrishna 🇼🇳
+ 1
Alright! ... so the doubt is that if we can't intialize the " String name" var in main as it wud be out of scope then how can we pass the parameter into a method called inside main as it wud be out of scope too, i guess. ***i mean that how passing parameter is possible but not the initialization for the same... passing parameter too is an initialization of var in itself, isn't sir? Note- ( *** is important )!!
4th Jan 2023, 12:32 PM
harsh singh
harsh singh - avatar
0
Am not able to understand your query actually!!. If possible, mention it with code sample like, Do you mean : public static void main(String[ ] args) { String name = "dev"; sayHello(name); // like this? It's ok. Works fine. This variable name is different from method parameter variable. Both not same... } }
4th Jan 2023, 2:14 PM
Jayakrishna 🇼🇳
0
bbdjbvfd hdbdkjojcs bbshdokkap kbsvsuutcve
4th Jan 2023, 7:10 PM
Mostafa Bahrami