Results 1 to 11 of 11
Thread: methods
- 11-30-2007, 05:10 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 14
- Rep Power
- 0
methods
MY PROBLEM: I dont know how to organize the main method.
The program has 5 methods.
The program has to start with the 1st method,
then go to the 2nd (typing this at the end of the 1st method:Java Code:secondMethod(NewVector);
The 3rd method uses an Array from the 2nd method.
Finally the 4th method has to start.
The 4th method needs an int calculation from the 5th method (the 5th method is 'private/public int' method with return).
The 4th method 'goes' to the 5th through this:Java Code:if(nameOfFifthMethod==-1){...}
So:
Java Code:public static void main (String [] args) {[B]what should be written here?[/B] }
Last edited by Zensai; 11-30-2007 at 05:20 AM.
- 11-30-2007, 05:42 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 18
- Rep Power
- 0
In main, create an object of the class and call the 1st method. from there call 2nd, and in 2nd call 3rd etc.
- 11-30-2007, 06:05 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 14
- Rep Power
- 0
why (=why we have to create an object) and how (we create an object, we call other methods)? thanx for your quick respond.
Last edited by Zensai; 11-30-2007 at 06:11 AM.
- 11-30-2007, 06:31 AM #4
Member
- Join Date
- Nov 2007
- Posts
- 18
- Rep Power
- 0
Object should be created to access any methods or variables inside the class.
alternative is that u can use static methods & variables but is not preferred in Java.
class Test
{
public Test()
{
}
public void Method1()
{
//u can call method 2 or any other method here
}
public void Method2()
{
}
public void Method3()
{
}
public static void main(String args[])
{
Test test = new Test();//Creating an object to initialize
test.Method1();//u can pass parameters here
}
}
- 11-30-2007, 06:50 AM #5
Member
- Join Date
- Nov 2007
- Posts
- 14
- Rep Power
- 0
Questions 1) why you created this empty method
Java Code:public Test() { }
Java Code:nameOfMethod2.(and inside here name of the string, arrays etc method 2 uses from method 1);
Java Code:method1.method2([I]parameters taken from method 1 and used in method 2[/I]);
can you please reply to them one by one? (it gonna be easier for me to understand them and faster for you to type them)
1000 thanx a priori!
PS java.lang.NoSuchMethodError: main
Exception in thread "main" :(Last edited by Zensai; 11-30-2007 at 07:26 AM.
- 11-30-2007, 06:54 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
You can add your main method anywhere inside the class. When you execute the program, it search the main method inside the class. There is no order to use them.
- 11-30-2007, 07:13 AM #7
Member
- Join Date
- Nov 2007
- Posts
- 14
- Rep Power
- 0
:confused:
- 11-30-2007, 07:25 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
When I go trough your questions, I feel that you are really newbie for Java. So it can be really helpful to you start learn those things first, with simple examples. I'll try to answer to you as much as possible.
There is no point. If you want you can do anything inside a method.
- 11-30-2007, 07:42 AM #9
Member
- Join Date
- Nov 2007
- Posts
- 18
- Rep Power
- 0
This empty method
public Test()
{
}
is called the constructor.
I prefer you to go through Java basics.
- 11-30-2007, 06:14 PM #10
Member
- Join Date
- Nov 2007
- Posts
- 14
- Rep Power
- 0
ive read what a constructor is, how about the rest questions?
- 12-03-2007, 06:31 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
3) why the main method is at the end?
When you run the application, it searches the main method first. No matter it is at the end or beginning.
Similar Threads
-
JSP methods example
By Java Tip in forum Java TipReplies: 0Last Post: 01-30-2008, 11:00 AM -
Friendly methods
By Java Tip in forum Java TipReplies: 0Last Post: 12-12-2007, 11:22 AM -
Methods
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 09:49 PM -
Using Deprecated Methods
By ravian in forum New To JavaReplies: 3Last Post: 11-23-2007, 08:58 PM -
Help with mathematical methods
By Marcus in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 09:20 PM
Bookmarks