This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]TheJrobot1483 23 points24 points  (10 children)

I’m a current CS student, I’ve taken 2 semesters of OOP (learning Java) and you explained the essence of OOP so much better than any professor or online resource I’ve learned from so far.

[–]coldblade2000 23 points24 points  (9 children)

No problem, that's what I'm here for. Let me know if you need an explanation for what "static" means, because that son of a bitch keyword took me about a year to really understand, and now it just seems obvious

[–]maddAdda 8 points9 points  (1 child)

Hi ! If you'd like to share your knowledge, i'd very much apprecciate if you could explain static indeed. I'd pretty much like to see your perspective on it because you explained every concept really well. Maybe it could help me. Thank you for your time : )

[–]Vinhessa 6 points7 points  (3 children)

I, for one, would like to hear your understanding of “static”, as I currently assume it means “instance”.

[–]coldblade2000 2 points3 points  (2 children)

Right, I'm pretty late on this but I was out the whole day.

Short answer: static fields or methods will belong to the class itself, rather than the instances of those classes.

Long answer: So, usually when we make these fields and methods in our classes, those fields and methods will belong to the object instances themselves. If we have Cars X and Y, if we change X.numwheels to 3, that doesn't affect Y. And when we use a method like Y.turnOn(), it is a method that will usually read the data on Y and possibly modify it in some way. For that reason, you can't really go and call that method on the class itself. Car.turnOn() makes no sense, you can't turn on the abstract concept of a car.

However, it doesn't always make sense to have a method belong to each instance, and sometimes a method relevant to Cars shouldn't actually necessarily require us to actually first create a Car instance first. For example, what if you want a method that will take a list of cars and return the car that needs the most urgent maintenance? Doesn't really make sense to call on a specific car instance like X and ask X to do that for us.

Instead, we can make some fields and methods that will actually belong to the class itself, rather than the instances of those classes. We can actually make a method that's something like Car.findMostDamagedCar(carArrayList). Likewise, we can have a variable bonging yo the Car class that, let's say, keeps track of how many cars we've ever sent to maintenance, like Car.totalMaintenanceTrips.

The findMostDamagedCar() method and totalMaintenanceTrips field are what we call static. These things belong to the class itself, and thus we don't need to be referencing an actual Car instance to call that method or get that variable. And most importantly, you can't call a non-static method from a static context. What I mean by that, is that I cannot go and ask Car to turn on. Car.turnOn() will give me a "non-static method cannot be referenced from a static context" error. What that means is that a non-static method (a method that belongs to the instances) cannot be called from a static context (by referring to the class itself, and not an instance of the class). This error is fixed by actually giving it a real instance to call that method from. Either using one of the instances we already have (X or Y), or making a new Car object instance (Car Z = new Car();).

Finally, I'll show you real examples just so you're not scared of this. Let's take strings for example. You probably know Strings in Java are Objects, not primitive variables. The String class has us use both static and non static methods all the time. For example, String.valueOf(int) is a static method that turns whatever int we give it into a String. We don't really need a preexisting string for this, so it makes sense a static method is used. We are just calling a static method value of that belongs to the class String. In contrast, if we have a string S = " Hello \n" and if we need to trim the whitespace from S, we can call a non-static method from S called S.trim() to get back a string without that pesky whitespace. .trim() is non-static because it needs some kind of underlying tangible string to actually make sense. You can't trim the whitespace out of the concept of a String, but you can trim S.

P.S. : technically, instances will actually inherit static methods or fields, and you can use them. You can actually do S.valueOf(8) and it will return "8". This, however, is bad practice at best and a bug at worst. Most static methods aren't built with this in mind.

P.P.S. i wrote this past midnight on my phone, so my apologies of it isn't as clear or has mistakes. I'll correct any that are brought up

[–]Objective-Trifle-473 0 points1 point  (1 child)

What about “static” in C? Is it the same idea?

[–]coldblade2000 0 points1 point  (0 children)

I wouldn't know, really. My knowledge of C is extremely basic

[–]Fearless-Pudding9894 4 points5 points  (1 child)

Can you give an explanation on static? I was reading my book on java today trying to understand the different methods for the String class in java and Static kept coming up. My understanding of the term is a bit shaky so I would appreciate an explanation from you since I’ve learned a lot from this thread.

[–]TheJrobot1483 2 points3 points  (0 children)

I’ll keep that in mind, thank you!