Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAditya Puri
1,080 PointsWhy didn't we declare an object for the class "System"??
Why didn't we declare an object for the class "System" the way we did it for the class "Console"??
For the class "Console" we had to first make an object called "console" and then call on the methods.
But in the case of the class "System", we just directly called its method without making an object first. Why is this so?
2 Answers
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsWhen you type System.exit()
you use static
method exit on Class System. For static methods like this you don't need to instantiate new instance of class. Start googling, from here:
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Aditya Puri
1,080 PointsAditya Puri
1,080 Pointsso the static methods do not need an object to be declared first? They can straight away be called in the class?
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsAlexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsExactly. Take a look at good reasoning when to use static methods (second best answer):
http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods
Very simple explanation for me, from there is this one:
Quote: "If any operation is not dependent on instance creation."