+ 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.
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.
+ 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
+ 3
https://code.sololearn.com/c5A195MAm3wz/?ref=app
+ 2
JaguaR my pleasure
+ 1
https://code.sololearn.com/couv0Ra3Xu3i/?ref=app
the Keyword "package" was making the problem
+ 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
+ 1
Martin Taylor thanks a lot, it's a great help for me.
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?
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?
0
Eashan Morajkar thanks a lot...
0
Atul thank you. It made things more clear.
0
Add static keyword to your class boy then it will excuted