Results 1 to 7 of 7

Thread: Methods

  1. #1
    tomelk31 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Methods

    I am having trouble understanding how to use methods. I have created an example of a program.
    I have a a constructor "Something" and then methods. How do I use the method setFirst() to set "first"?
    Also I had two classes how would I use setFirst() in a different class?


    Java Code:
    public class  Something{
    	
    	private String first, second;
    	
    	public Something(String f, String s){
    		first = f;
    		second = s;
    	}
    
    	public void setFirst(String f){
    		first = f; 
    	}
    	
    	public void setSecond(String s){
    		second = s;
    	}
    
                 public static void main(String[] args){    
    
                 }
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,582
    Rep Power
    25

    Default Re: Methods

    how would I use setFirst() in a different class?
    Create an instance of the Something class and use its reference to call the method.
    objRef.theMethod(theArgs);

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Methods

    Remove the main method from the class.
    You will create an instance of the class from another class.
    Now to use the setFirst method as Norm said you have to create an instance of something class such as:
    Something s1 = new Somthing("hello","hello2");
    Type s1. and then all the something class methods will be showen.
    You might find some other methods in there that you didn't make, because every class inherits from the object class his methods(Such as equals,toString,init etc..).

    edit:
    I forgot to mention,
    when you run methods on an instance you should use "this".
    For example: this.first = s;
    This means the current object, if the parametter of the method was first for example your method was looked like this:
    Java Code:
     public Something(String first, String second){
            first = first;
            second = second;
      }
    You don't want that to happens
    Yea it's true if you will use diffrent parameters names everything will be fine, but my suggestion is to get used to it(using this).
    Last edited by tnrh1; 11-10-2011 at 03:53 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,582
    Rep Power
    25

    Default Re: Methods


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

    Default Re: Methods

    Quote Originally Posted by tnrh1 View Post
    Remove the main method from the class.
    Why? It doesn't matter where the main method is.
    Type s1. and then all the something class methods will be showen.
    Only if you are using an IDE and it has that feature.
    when you run methods on an instance you should use "this".
    Once again why? I rarely use "this" in my programs.
    Yea it'strue if you will use diffrent parameters names everything will be fine, but my suggestion is to get used to it(using this).
    We disagree. I have seen code where "this" is used before every usage of an instance variable.
    Java Code:
    public void doStuff() {
        this.value = this.otherValue - 2;
        this.text = this.text.substring(this.value, this.text.length());
        // etc
    }
    It gets very ugly, very quickly.

  6. #6
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Methods

    It makes more sense to create an instance of a class outside of it. That's the way I learned it by studying from books and school.
    of course you can make an instance of a class inside her, but atleast for me it looks better to create it outside of it.
    And about "this", you show an excessive example but that doesn't negate the fact that it exists.
    I think it more easy to understand the code with "this" and might prevent future errors as I mentioned:
    Java Code:
    public Something(String first, String second){
           first = first;
           second = second;
     }
    Java gives you a toolbox, use it as you wish.

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

    Default Re: Methods

    Quote Originally Posted by tnrh1 View Post
    It makes more sense to create an instance of a class outside of it.
    Why does it make sense. It makes no more sense than having a main method within the class. In fact you would probably find there are plenty of classes in production that have main methods that were used for testing purposes and never removed. One reason they are not removed would be for regression testing.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  3. Replies: 1
    Last Post: 10-05-2011, 12:15 AM
  4. Using methods?
    By erickGotJava in forum New To Java
    Replies: 7
    Last Post: 04-10-2010, 03:58 AM
  5. Replies: 4
    Last Post: 04-03-2009, 06:36 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
  •