• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Alternative Method for Calling Methods?

 
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I get how you can call a non-static method from a static method within the same class using something similar to this...

And I get that you can use that same method for going class-to-class methods... but is there a faster way? My code looks all sloppy from creating all these references It just doesn't seem logical to keep creating references to navigate through a class's methods, I had to create 2 of these just to get back to 1 method in my primary class from 2 different classes :\
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...


Right, well first off:
firstly first = firstly(); // creates reference
is wrong. It does NOT create anything (unless there's code that you haven't included).

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.

Winston
 
Levi Neuxell
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:firstly first = firstly(); // creates reference
is wrong. It does NOT create anything (unless there's code that you haven't included).


So if it's not creating anything, that must mean that it's simply a reference in itself - or is this wrong?

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.


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. Thank you for your response though.
Edit: Just putting this out there (on that first 101): my program does work, which is why I want to know if there's a better method :3 But still, it's a very small, very simple program, so I guess speed really doesn't matter much haha.
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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


The latter. If you want to create an instance of a firstly (and BTW, it should be Firstly), you have to write
new firstly()

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).

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.

Winston
 
Levi Neuxell
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()


What is an instance, anyways? (This is beyond what I've been taught in school. =p)

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).


Right, but still, do you know of any alternative methods/syntax?

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's an example program. Sorry, but you're right, I haven't show you my actual program.
Here's what it looks like from my program:

 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Levi Neuxell wrote: . . . What is an instance, anyways? . . .

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. The short and stout fields can be added later. That is an instance of the Teapot class, and the bit of memory next to it containing something saying it is a Teapot containing 30 of tea is another instance of the Teapot class.
Saying something is an instance of class X is very similar to saying it is an object created from class X, but all Java objects (with one notable exceptional case) are created from at least two classes together. To get that sort of special case you can write new Object().
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Levi Neuxell wrote:What is an instance, anyways? (This is beyond what I've been taught in school. =p)


An instance is exactly the same thing as an object. The word "instance" is usually used when talking about an object and its class - an object is an instance of a class.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Levi Neuxell wrote:Here's what it looks like from my program:...


OK well, once again, class names should start with CAPITAL letters and use CamelCase. It's only a convention, but it's so pervasive as to be almost be a 100% rule, so it should be CarWash, not carwash.

Other than that, I think my colleagues have provided with plenty of good advice to be going on with.

Winston
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



If you are thinking procedurally, and/or the problem lends itself to a procedural solution, then yes, creating all these objects is overkill. You might as well put all your code in the main method and/or modularize the code into static methods that are called from the main method

However, when you are thinking from an Object Oriented perspective, then your classes will reflect the way you think about the problem. The creation of the objects won't feel "sloppy" because it reflects how you think about things. Usually, (that means not always) a lot of real world problems lend themselves naturally to an Object Oriented solution. Also, using an Object Oriented solution provides more flexibility. It's like building with Legos. You build these blocks that fit together, and if something changes, you just take a block out and put another one in it's place. Using the same blocks, you can build anything.. a house, a spaceship, batcave...

IMO, if you are just starting to learn, don't worry about Object Oriented concepts right now. First, you need to learn how to put blocks together, then you can understand why blocks are designed the way they are, and then you can start designing your own blocks. Right now, have fun playing with the blocks.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the risk of having to split myself off this thread, I would say that lots of the people we see here do have difficulty with OO concepts. It is a lot easier to understand an if‑else or a loop than why you should have an object.
 
Levi Neuxell
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what you guys are basically saying is that I need to suck it up and just deal with having all these references?
An example of what's causing some of this confusion is in this code, where I thought that establishing a point of reference in the class would allow its use in each method:


So I have to do this:

Other than making each method static (which I've been told is bad coding), I can't think of any alternatives.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you got the car wash class working yet? Until you have done so, forget about the other classes.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Levi Neuxell wrote:



It seems like you have been asking the wrong question. Instead of asking for different ways of calling methods, you should be asking why the calls you are making aren't working. The answer depends on what 'doesn't work' means. what error message do you get? what is the actual code which causes the error?
 
Levi Neuxell
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Levi Neuxell
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 creating establishing all these reference [points]...



And in the title:

Alternative Method for Calling Methods?



(I accidentally double-posted while editing somehow... but it's too late, so I filled this one up with quotes.)
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Yes, but I'm looking for wondering if there's an alternative to that (creating objects for every single method to refer to other non-static methods). =3

I know how to call static methods, I know how to call non-static methods; I'm wondering if there's a different, shorter way to call non-static methods (like a shortcut).


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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


It's not a good idea to get shirty with people who are trying to help you.

You've already been told more than once that an instance method requires an instance. Therefore, there's no "best" about it - YOU MUST CREATE AN INSTANCE.

As to your second question which, from what I can gather (because it's not obvious), is: Do I have to do this in every method? The answer is NO; that's what instance variables (or constants) are for.

So now, I suggest you read up about them, because putting static on everything is not the answer.

Winston
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don’t go round editing posts after they have been replied to, LN.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Perhaps you didn't understand the point I made. You said that you want to do this:

But 'it didn't work. So you end up doing this:

Which does work. The difference is that you create a new carwash object in each of the methods, whereas you want (as in your first example) to create it just once and re-use it. Rather than asking why you can't re-use the same object you ask about alternative ways of calling methods.

You have been told there is no alternative way to call methods, and you understand that. But that seems secondary to me. It seems like you originally wanted to use the first code snippet and only had this efficiency / alternative question because the first snippet didn't work. So shouldn't you be wondering why the first snippet didn't work? Maybe if you understand why it didn't work you can make it so it does work - and then this efficiency / alternative issue goes away?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Levi Neuxell wrote: . . . I've looked in my Java book, . . .

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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:




It's not clear to me what the OP's actual question is at this point in the discussion, or his context. If he's already in a non-static context (in the body of a constructor, instance initializer, or instance method), then yes, he can call the instance method on "this". He doesn't even need the explicit this qualifier. Simply calling menu() is fine, and in fact is preferred.

If he's in a static context though, then obviously that won't work, as there is no "this."
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...



The key point is that most of the time you won't be in a static context. If that's not the case, then you have a design flaw. But when you are in a static context, the only way to call a non-static method is to obtain a reference to an object of the type in question. Maybe it was passed to you as a parameter, maybe you create it on the fly, maybe you fetch it from a cache. But before you do that, you have to ask yourself why you're in a static context now and why you want to call a non-static method at that point. Digging into those questions may uncover design problems that, when fixed, make this static/non-static issue go away.
 
Levi Neuxell
Ranch Hand
Posts: 41
Eclipse IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is pretty much resolved, I got my answer. Sorry, didn't mean to seem rude - when I was thinking of those posts it wasn't with a serious tone, but instead with a joking manner. :3 Still, I have a lot to learn and that is really obvious. Thank you all for being patient and helpful though.
 
reply
    Bookmark Topic Watch Topic
  • New Topic