Results 1 to 7 of 7
Thread: Methods
- 11-10-2011, 02:42 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
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){ } }
- 11-10-2011, 03:16 PM #2
Re: Methods
Create an instance of the Something class and use its reference to call the method.how would I use setFirst() in a different class?
objRef.theMethod(theArgs);
- 11-10-2011, 03:47 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
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:
You don't want that to happensJava Code:public Something(String first, String second){ first = first; second = second; }
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.
- 11-10-2011, 03:53 PM #4
Re: Methods
Have you looked at the Java tutorial?
Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)
- 11-10-2011, 10:38 PM #5
Re: Methods
Why? It doesn't matter where the main method is.
Only if you are using an IDE and it has that feature.Type s1. and then all the something class methods will be showen.
Once again why? I rarely use "this" in my programs.when you run methods on an instance you should use "this".
We disagree. I have seen code where "this" is used before every usage of an instance variable.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).
It gets very ugly, very quickly.Java Code:public void doStuff() { this.value = this.otherValue - 2; this.text = this.text.substring(this.value, this.text.length()); // etc }
- 11-11-2011, 12:33 AM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
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 gives you a toolbox, use it as you wish.Java Code:public Something(String first, String second){ first = first; second = second; }
- 11-11-2011, 12:44 AM #7
Re: Methods
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
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
difference between static methods and instantce methods?
By venkatch in forum New To JavaReplies: 1Last Post: 10-23-2011, 12:37 PM -
Incorporating If-Else into Methods + Private Helper Methods?
By 5minutes in forum New To JavaReplies: 1Last Post: 10-05-2011, 12:15 AM -
Using methods?
By erickGotJava in forum New To JavaReplies: 7Last Post: 04-10-2010, 03:58 AM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks