Levi Neuxell wrote:So I get how you can call a non-static method from a static method within the same class using something similar to this...
but is there a faster way? My code looks all sloppy from creating all these references...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:firstly first = firstly(); // creates reference
is wrong. It does NOT create anything (unless there's code that you haven't included).
Winston Gutkowski wrote:
but is there a faster way? My code looks all sloppy from creating all these references...
Programming 101:
1. DON'T WORRY ABOUT SPEED - at least not until you have a working program first.
2. "Sloppy" is in the eye of the beholder. Java is an object-oriented language, and in order to use objects, you need to create them. If you don't like it, or think that it looks sloppy, then maybe Java isn't for you.
Levi Neuxell wrote:So if it's not creating anything, that must mean that it's simply a reference in itself - or is this wrong?
Apologies for the mistake, but by 'sloppy' I mean it feels kind of useless to right all of these references...
Edit: Just putting this out there (on that first 101): my program does work...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:If you want to create an instance of a firstly (and BTW, it should be Firstly), you have to write
new firstly()
Winston Gutkowski wrote:
Apologies for the mistake, but by 'sloppy' I mean it feels kind of useless to right all of these references...
Well how do you imagine that you're ever going to get a program to do anything? - beyond printing "Hello world" that is (and even that relies on an existing System class).
Winston Gutkowski wrote:
Edit: Just putting this out there (on that first 101): my program does work...
Then, as I said, it contains code that you haven't shown us; because that statement I mentioned will NOT compile.
It is a set of bits of +/- or north‑south poles in your RAM, which represent an object created from that class. You never say an instance, you say an instance of class X. You might have a Teapot class, which has a field saying how much tea it contains. You can have an instance of the Teapot class with that field filled in with 5 (or more precisely 0000…0101), which simply tells the JVM that it is a little Teapot containing 5.Levi Neuxell wrote: . . . What is an instance, anyways? . . .
Levi Neuxell wrote:What is an instance, anyways? (This is beyond what I've been taught in school. =p)
Levi Neuxell wrote:Here's what it looks like from my program:...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Levi Neuxell wrote:
Apologies for the mistake, but by 'sloppy' I mean it feels kind of useless to right all of these references, and I'm just wondering if there's a better, or at least an easier way of doing it. If not, then it's not a problem.
Levi Neuxell wrote:
Steve
Other than making each method static (which I've been told is bad coding), I can't think of any alternatives.
Right, but still, do you know of any alternative methods/syntax?
I also changed the topic title from "More Effective Method for Calling Methods" to "Alternative Method for Calling Methods", as I guess I'm not looking for more efficiency but rather an easier approach.
but is there a faster way? My code looks all sloppy from
creatingestablishing all these reference [points]...
Alternative Method for Calling Methods?
Levi Neuxell wrote:Actually, I'm asking the right question. My program works, I'm just looking for an easier - or perhaps more efficient - way of calling non-static methods (which is what I've repeated about 3 or more times now). There's really no problem at all, it's just a simple question that no one seems to be able to grant a straight answer.
Paul Clapham wrote:
Levi Neuxell wrote:Actually, I'm asking the right question. My program works, I'm just looking for an easier - or perhaps more efficient - way of calling non-static methods (which is what I've repeated about 3 or more times now). There's really no problem at all, it's just a simple question that no one seems to be able to grant a straight answer.
Actually the answer has been given in this thread. Several times, if I'm not mistaken. To call a method of a class which isn't static -- an instance method, in other words -- you simply create an object which is an instance of that class, and call the object's method. It can't get any easier than that.
Levi Neuxell wrote:A simple 'no, creating objects in each method and calling the object's method is the only (or at least the best) way' would have sufficed. Thank you.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Levi Neuxell wrote:Actually, I'm asking the right question. My program works, I'm just looking for an easier - or perhaps more efficient - way of calling non-static methods (which is what I've repeated about 3 or more times now). There's really no problem at all, it's just a simple question that no one seems to be able to grant a straight answer. All my classes have always worked (until now, while I'm trying to call non-static variables from different non-static methods). I've looked in my Java book, and every example for calling a method uses static - hence why I came here for an answer.
Steve
There are unfortunately some books which claim to teach Java and don’t teach object‑orientation. Yours must be one of them. What is it, please?Levi Neuxell wrote: . . . I've looked in my Java book, . . .
Chip Furstenau wrote:I'm still new around here but this seems like a simple fix to me. Using the "this" keyword is an alternative way to call a non-static method without creating a new object each time. Like so:
Levi Neuxell wrote:So I get how you can call a non-static method from a static method within the same class using something similar to this...