[ANSWERED]Method inside main | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[ANSWERED]Method inside main

I get errors when creating those. Can I really not create methods inside main? I need to include the data inside the main (which has to be in main because it includes creating objects from an outer class).

1st Apr 2017, 2:10 PM
Alex Snaidars
Alex Snaidars - avatar
8 Answers
+ 1
Do you mean creating methods in public static void main(String[] args) ?
1st Apr 2017, 2:16 PM
Hassie
Hassie - avatar
+ 1
Here's an example of passing data from main and retrieving it back. https://code.sololearn.com/c4cUSQN6uuAC/?ref=app
1st Apr 2017, 2:27 PM
Hassie
Hassie - avatar
+ 1
Looks good. The code that you want to implement, i think its possible, I'll see if i can implement it in some way.
1st Apr 2017, 7:33 PM
Hassie
Hassie - avatar
0
@Hassie I do.
1st Apr 2017, 2:17 PM
Alex Snaidars
Alex Snaidars - avatar
0
ok, yeah you can't create a method within a method. That would be true for any Programming language. You declare all methods in classes. Note: Because main is a static method, you must make any methods static to use them in the main static method. What you can do is create methods which take in parameters and return the value. I'll create a simple code and link it.
1st Apr 2017, 2:18 PM
Hassie
Hassie - avatar
0
@Hassie You mean setters and getters stuff?
1st Apr 2017, 2:25 PM
Alex Snaidars
Alex Snaidars - avatar
0
You could do, by creating variables which are accessible by the whole class. Or just create function methods, pass the values as parameters and return the value back.
1st Apr 2017, 2:32 PM
Hassie
Hassie - avatar
0
@Hassie Would you take a brief look at my code? There are many repetitious elements but it's short indeed without repetition. https://code.sololearn.com/c6Sx0C18ue7W/?ref=app
1st Apr 2017, 4:34 PM
Alex Snaidars
Alex Snaidars - avatar