Programming in Java

Discussion in 'OT Technology' started by hurleyint1386, Sep 11, 2008.

  1. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    So I've been programming in C++ for a while now, and I'm taking a course at another college, and it's using Java. Our first assignment is extremely simple, but I can't figure it out! Pretty much we went over setting things up. We are creating a dynamic array, and each person got a little chunk to do. Mine was "Find out remaining slots in current array" Which is just capacity-size. But I can't figure out methods for java. I keep getting "non-static method numAvailable() cannot be referenced from a static context". I checked http://mindprod.com/jgloss/compileerrormessages.html#NONSTATICCANTBEREF and tried a bunch of their suggestions but nothing is changing. If anyone could help, that would be awesome. Let me know if you'd like to see the existing code.
     
  2. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Your class is static, and you are calling non-static methods.
     
  3. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    So how do I set the method to be static? public void static numAvailable?

    Edit: if I do that, new error:

    DynArray.java:107: <identifier> expected
    public void static numAvailable()
     
    Last edited: Sep 11, 2008
  4. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Make the class non-static. It sounds like you are doing shit in public static main. Don't. Make a class, and use it. Then you won't have that issue.

    You know C++, you should be able to handle that.
     
  5. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    If I were using C++, this would be done lol not that I'm a C++ expert, but I know it enough to be able to get something like this done

    I am using public static main, but that's what was given to us in the class. So I assume I have to stay within that. Here's what we were given:

     
  6. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    I don't see numAvailable in there, which is probably why she didn't have that problem. You can't just call a non-static function from main, if it belongs to a non-static class. Instantiate the class, and invoke the method via the object.

    Just like you might do in C++. Java is C++ for tards.
     
  7. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Sorry, I took numAvailable out so you could see everything that was actually given to us. I'll try what you suggested and see what I can do. Thanks for the help
     
  8. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Alternatively, make the method static, if it can be.
     
  9. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    So I created a static method, and it works. Now I need to work on accessing information from the constructor
     
  10. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    I don't believe you can do that in a static method.
     
  11. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Dammit... So I don't know how to access the capacity or the size. I guess the only way to do it would be if I don't make main static, then pass the variables in? But there's got to be another way because otherwise, the teacher wouldn't have made main static
     
  12. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Hmmm, if I make main non-static, then it compiles, but doesn't run due to "NoSuchMethodError: main"
     
  13. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    You can't make main non static.

    Here is your solution:

    1) Have a class that is not static, and a method that is not static.

    2) Instantiate that class as an object in main.

    3) Call the class's methods to do your work. Those methods have access to the instance variables and is super cool.

    ...

    PASS CLASS!
     
  14. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    I created a class named Remaining which is not static, and the method numAvailable within Remaining which is also not static. When compiling, I get:

    DynArray.java:103: non-static method numAvailable() cannot be referenced from a static context
    Remaining.numAvailable();

    Sorry I'm taking up your time
     
  15. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Thats because you are calling the function directly from main instead of as an instance method?
     
  16. hurleyint1386

    hurleyint1386 Someone has sand in their vagina

    Joined:
    Jan 6, 2005
    Messages:
    3,687
    Likes Received:
    0
    Location:
    Rochester, NY
    Thanks a lot for all your help. I got it running.
     
  17. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Cool!
     
  18. SIGirl

    SIGirl Super Duper Moderator Super Moderator

    Joined:
    Nov 1, 2001
    Messages:
    22,666
    Likes Received:
    1,350
    Location:
    Austin, TX
    EEP I have Java next quarter. Just took my C# final today. Is Java very different?
     
    Narc likes this.
  19. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Just the libs.
     
  20. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    You should be able to make the switch reasonably smoothly language wise, but you will probably have teething problems with changing IDEs, losing a lot of the syntactic sugar C# has etc.

    If you learned C# 3.0 then Java will probably be more painful in comparison.

    http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java
     
  21. CodeX

    CodeX Guest

    :rofl:
     
  22. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Well, if you 'Know' 20% of the features of C++, you shouldn't have problems with Java's concepts, ya know?
     
  23. CodeX

    CodeX Guest

    No I agree... Java is like the idiots hold-my-hand-the-whole-time-and-dont-let-me-do-anything-fun version of programming
     
  24. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    I don't agree at all. C++ is the 'too many features (so many NO ONE knows them all), doesn't scale well for large projects, only use if you have a VERY good reason to do so' language. Its so much easier to get things done, in most spaces, with Java. Especially with a team.

    Building slot machines in Java/C++... which project do you think is likely to come in on time and under budget? The C++ projects are never on time. The Java ones are. This is not a coincidence.

    That being said, if you know C++ Java should be easy, as it has far fewer features.

    Real world example: Write a simple protocol server to Send and recieve SOAP messages in C++. Now try it in Java. Tell me how long it took you. Get it now? :)
     
  25. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    How the fuck does C++ not scale well?
     

Share This Page