Results 1 to 7 of 7
  1. #1
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Accessing class, the right way

    Hi guys,

    My first post:), you all participate in a fantastic forum and i am happy to be apart of this learning experience.

    I am fairly new to Java; having played around with it for a few months before now studying it at Uni (have just started). I have completed an assignment in two forms. I have completed it using methods in a procedural fashion, as this is what i am comfortable with. I have also re-done this assignment using classes.

    My question is this:

    I have called the classes by creating an object (as you do) and simply accessed them in the same fashion as you would a method in a procedural program. for example:

    MyClass myClass = new MyClass(arg1, arg2);

    This works fine and my program runs without a hitch. But is it this form exceptable? should i instead be returning values from my class arguments. The classes themselves are for printed menus and mathematical equations etc..

    I guess im asking this because although this works fine, i get a little warning message on my coded work (eclipse) stating that this local variable is never read. I understand what the warning is saying, i am just looking for ideas on whether the way i have accessed these classes is acceptable or is it frowned upon.

    I would appreciate any thoughts on this subject.

    Cheers

    BM

  2. #2
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    MyClass myClass = new MyClass(arg1, arg2);

    This works fine and my program runs without a hitch. But is it this form exceptable? should i instead be returning values from my class arguments. The classes themselves are for printed menus and mathematical equations etc..
    I am new in Java also.According to my knowledge, MyClass(arg1, arg2) is a constructor having too parameters.In your class definition there must be a constructor receiving two arguments. And a constructor has no return type.

    About the warnings,Can you show them ?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Yes and no err maybe.

    Creating an object and calling its methods is obviously the correct way to go otherwise how would you get the code in the method to execute? However what a method does and what value it returns depends upon what your overall goal is. For example if you have a Circle class you could have a method to calculate the area. Now does that method simply calculate the area of the Circle and store it in an instance variable? Or does the method calculate the area and return it to the caller who can then decide what to do with it? Or should the method calculate the area and then display it? :shrugs: The only person who can answer that is the person writing the code.

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    I am new in Java also.According to my knowledge, MyClass(arg1, arg2) is a constructor having too parameters.In your class definition there must be a constructor receiving two arguments. And a constructor has no return type.
    Yes, this is absolutely correct.

    You can then call methods from the class's object you just created like so:

    Java Code:
    otherClass.doMethod();
    and then it will do the method in class otherClass.

    You can also extend the parent class by writing for example:
    Java Code:
    public class One extends Two {
    and then you can just write doMethod(), you won't need to make instances of the Two class (create objects).

  5. #5
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Thats pretty much what im using, the method is inside a constructor. i create the constructor first as in :

    public Something(){
    }

    and then i use this again with :

    public Something(){

    filled with method

    }
    the program works fine, but is this the wrong way to be doing it?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    For starters you cannot have a method inside a constructor. If you mean call a method inside a constructor, then yes you can do that. But as I tried to explain earlier there really isn't a right or wrong way to do things. There are certain standards that people should abide by (class names start with capital letter, method names start with lowercase letter) but at the end of the day if your code compiles runs and produces correct output then it is OK. Of course as you gain more experieince you will learn that some things can be achieved in a more efficient manner.

  7. #7
    BrainMelt is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks for taking the time to answer my question, i really appreciate it, and as a noob im grateful to be able to speak to experienced people, it makes a difference when you can ask questions and get someone with experience to clarify, instead of just assuming something is OK.

    Cheers

    BM

Similar Threads

  1. accessing a singleton class inside another class
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-08-2010, 06:23 PM
  2. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  3. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  4. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 AM
  5. Replies: 5
    Last Post: 01-03-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •