• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Returning value from one method as input to another

 
Ranch Hand
Posts: 208
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have watched a few videos on returning values with methods and done  few exercises

im now trying to take input from one method and use it in another
i simply want a number from a user and then run that number through my isEven method
but it seems like when iput the variable from userInput into isEven its 0




Does the value i want the isEven method to work on need to be returned from the userInput method?
or can it simply be a variable that puts the value into the method.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use static variables and static methods just for getting rid of creating objects. That way, you are actually keeping yourself away from the intent of OOPS paradigm.

So basically you need a value from a user from some method and wants to pass it to other for some processing. In your code it's not working because you have not called the other method(isEven, which needs to do the processing of user input).In java for executing some method you have to call it in some context like the main method(or some other one). and please note their is not any relation b/w the parameter declared in method, isEven(named as userNumber), and the one declared as a class static variable(also named as userNumber). You can indeed name them something else different from each other.
you can call isEven as shown, in your main method:
 
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. As shown, this code simply echoes the value entered by the user

2. The isEven() method is never called

3. The isEven() method is confused/misleading -- a name like that implies a boolean return value. However, you have it returning an int. In fact, you have it always returning 0. In normal conversation, if someone asked you "Is 3 an even number?" would you expect them to answer "0" or "No"? Would you expect "0" to be the answer to "Is 4 an even number?" That's what your isEven() method would do. But then again, since it is never called in this program, it doesn't do anything.
 
Junilu Lacar
Sheriff
Posts: 13517
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

praveen kumaar wrote:Don't use static variables and static methods just for getting rid of creating objects.


I'm not sure how you came to the conclusion that "getting rid of creating objects" was the motivation behind OP's using static methods. Maybe he doesn't know any better?

At any rate, we probably shouldn't make any of those kind of assumptions. You could just ask OP what his reasons are for using static members instead of instance members.
 
Marshal
Posts: 64680
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP is probably making things static because I told him on Tuesday to create a utility class. A utility class usually has no usable data in itself, so it never needs to have any instances and all its members are static.
But you haven't written a utility class; you have simply put some blocks inside the main() method, which is is there for starting your application and nothing else. Not at all a good thing to do. What't more, you have made a very complicated looking loop, even though I showed you two much simpler versions of that loop/method on Tuesday night, using the technique Rob Spoor taught me ages ago. Notice that loop never throws an exception because you have entered incorrect input.
Don't try copying that code into a main() method. Create a utility class, with the following features:-
  • 1: All members static.
  • 2: Never create any instances; you never need them. See the Java® Language Specification (=JLS). Give the class one private constructor and never call it. That has the effect of preventing you creating subclasses, too.
  • 3: Don't allow your method to have any side‑effects, as far as possible.
  • 4: Every non‑private member should have /** documentation comments */ to explain how it works and how to use it. In fact my class comprises about 75% documentation comments.
  • If you had written a utility class with a private constructor and a private Scanner field, you could have copied my two methods and had something running. As it is, you have written something more complicated, which you can't reuse.
    The techniques for getting next() from the jeyboard, and nextLine() are rather different; I now write nextLine() differently than I used to.
    Have we told you off about naming things before? Names like MethodReturnEvenNumber are very confusing and hardly accurate.
     
    wayne brandon
    Ranch Hand
    Posts: 208
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi guys

    This was just an exercise I set myself to get better acquainted with methods returning values etc etc
    i made things static as just to test a little exercise i dint think it necessary to create an object of the class etc
    but after typing this i realise that I should implement and do everything "properly" as that is the only way im going to learn.

    we had a habit of making all variable static when i took a class, its a habit i should get out of
     
    wayne brandon
    Ranch Hand
    Posts: 208
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    this is my Utiliy class campbell


     
    wayne brandon
    Ranch Hand
    Posts: 208
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also Campbell was reading this:

    Preventing Instantiation of a Class

    A class can be designed to prevent code outside the class declaration from creating instances of the class by declaring at least one constructor, to prevent the creation of a default constructor, and by declaring all constructors to be private.

    When would code outside a class declaration create instances of a class? is this normally just a mistake from another programmer if you were working on a project for example?
    Does it just stop the class being instantiated so that no factors at all can make an object from it?
     
    Junilu Lacar
    Sheriff
    Posts: 13517
    223
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Generally, you do want to design classes that can be instantiated from client code. Most of the standard library classes are like that, like ArrayList, HashMap, Random, etc. There are also times where you'll use static factory methods. It all depends on how you want your class to be used.

    For utility classes that have only static methods, it doesn't make sense to provide a way to instantiate that class because it w
    doesn't have any instance members. In that case, it's sensible to make constructors private.
     
    Campbell Ritchie
    Marshal
    Posts: 64680
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    wayne brandon wrote:. . . When would code outside a class declaration create instances of a class? . . .

    There is no such thing as code outside a class declaration (well to a first approximation), so you probably mean when would code in a different class create instances? Answer: all the time.If you look in the JLS link I showed you yesterday, it tells you exactly when you can and cannot instantiate a class with private/default access (=package private) constructors.
    Afraid your class won't work. Its methods don't “get” anything; they simply consume keyboard input  You want to be able to say...and get an int in the range 123...456. That looks exactly like what I showed you on Tuesday, and I have told you the methods to write, well a few of them. I have also told you to write a private constructor. The one part of that class I am happy about () is line 7. Use that class to get ints from the keyboard and work at it until it works, which it will if you follow my suggestions. Get that working for ints, then consider other inputs later. Adding methods to get longs, BigDecimals, booleans, etc. is about the same difficulty, and plain simple next() is easier. Writing nextYesNo(), or nextLine() are slightly more difficult. You can add such methods whenever you need them.
     
    wayne brandon
    Ranch Hand
    Posts: 208
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "instantiated from client code" what does that even mean?
     
    Junilu Lacar
    Sheriff
    Posts: 13517
    223
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    wayne brandon wrote:"instantiated from client code" what does that even mean?


    "Client code" is simply code that's not defined in the class that you're using. The code that you write that uses standard library classes like ArrayList and HashMap, for example. "Client" code is simply code that uses the services provided by other code.

    All that code can be considered "client code".
     
    Campbell Ritchie
    Marshal
    Posts: 64680
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Client code is any code using that particular class.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!