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 trialOshedhe Munasinghe
8,108 PointsI dont understand these static in method and instance variable
What does static mean?
I thought static means for method main(String[]args) like that.
and explain me these:
public static boolean x;
public static String hello(){..}
2 Answers
Kevin Frostad
3,483 PointsIf you make an object static, you're telling the object that it belongs to the class, and not an instance of the class. That means you can call the object directly from the class, without making an instance of it. If you have a class Car, then upon calling the constructor made a blue car and a red car, you would have two objects of that class (instances). So if you were to have a static method who keeps count of, say.. all the colors of every car you've made. Then instead of belonging to the object of that class, giving you only the color of the current object, it would give you the color of every car you have made. Just think that it belongs to the class, not the instances you would make of it.
I would recommend searching oracle or just google it for questions like this, there's a lot of good explanations out there. Of course it's hard to learn all the concepts around java, so I understand if boring oracle documentation sometimes isn't enough. Here's a tutorial from oracle about static: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Seth Kroger
56,413 Points"static" means if belongs to the class as a whole, not just a particular object. It means that:
- It's value is the same for all objects of that class, unlike instance variables which can be different from object to object.
- It can be accessed from it's class without referring to a particular instance, which is useful for constants and methods when needed.
Kevin Frostad
3,483 PointsKevin Frostad
3,483 PointsSay your public static String hello(){} contained the static boolean x, which returns a string. So "if x == true; return "Red car!"; else return "No red car..";". You would of course need to make a method which made "x" return true whenever you make a red car.
Oshedhe Munasinghe
8,108 PointsOshedhe Munasinghe
8,108 PointsDude, Thanks a lot for that link. True, to read oracle doc is a bit boring but that was really worth it. It took me to understand what you mean about the word "instance". NOW I understand about static and what the different between Instans Method and Class Method! :)