+ 1

plz help me resolve the errors, I am not even understanding what the error is.

https://code.sololearn.com/cA22A9A8a24A I created a constructor named boy and trying to access his height attribute.

31st May 2021, 11:37 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
15 Respuestas
+ 3
So the first change i made was that the main function '{' was closed after the boy class but as it is a separate function so it should have been closed before the start of the boy class. Second was that you are making an object of boy class inside the boy the boy class so the answer was that you have to write it inside it inside the main function and also your print function should be written inside main.
31st May 2021, 11:52 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 3
public class myClass { public static void main(String[] args) { System.out.println("welcome"); boy b1 = new boy(); System.out.println(b1.height); } } public class boy{ int height; boy() { height = 22; } } //This can be the most simplest way
31st May 2021, 2:20 PM
Atul [Inactive]
+ 3
https://code.sololearn.com/c5A195MAm3wz/?ref=app
1st Jun 2021, 3:01 PM
Razan
Razan - avatar
+ 2
JaguaR my pleasure
1st Jun 2021, 5:20 AM
Atul [Inactive]
+ 1
https://code.sololearn.com/couv0Ra3Xu3i/?ref=app the Keyword "package" was making the problem
31st May 2021, 11:44 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
You cant make an object of a class which it is in so it should be written in the main function and the print function was taken there because you wanted to print the height and that can only be accessed by the boys object
31st May 2021, 11:56 AM
Eashan Morajkar
Eashan Morajkar - avatar
+ 1
Martin Taylor thanks a lot, it's a great help for me.
31st May 2021, 1:00 PM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
0
Eashan Morajkar thank you, I modified it and it's still showing the same error. I have seen your code, and it has significant changes. Can you help me what I need to change?
31st May 2021, 11:49 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
0
Eashan Morajkar one thing I noticed is boy b1 = new boy(); Syste.. (b1.height); These 2 lines were moved into main method and one of my error disappeared, it means can't I have them in another sub class?
31st May 2021, 11:53 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
0
Eashan Morajkar thanks a lot...
31st May 2021, 12:10 PM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
0
Atul thank you. It made things more clear.
1st Jun 2021, 5:06 AM
Jagadeesh Gurana
Jagadeesh Gurana - avatar
0
Add static keyword to your class boy then it will excuted
2nd Jun 2021, 10:08 AM
Joshi Mucheti