I am new to Java but I know how Javascript works. Can somebody please explain to me how to use methods in Java like functions in Javascript. What is the difference between Methods and Classes in Java? Where and when should I implement them? Sorry if this is already posted or shouldn't be asked here. I can't find a clear example I can understand online. Thanks again!
11 Replies - 613 Views - Last Post: 24 June 2011 - 06:33 PM
#1
How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 12:58 PM
Replies To: How to use methods in Java like functions in JavaScript
#2
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:02 PM
Method is the word used in the OO world to refer to a function or subroutine in other languages.
Methods belong to a class and you need an instance of that class to access the method unless it is declared static.
For the rest of your question is will take an whole book to answer.
Methods belong to a class and you need an instance of that class to access the method unless it is declared static.
For the rest of your question is will take an whole book to answer.
#3
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:02 PM
staley412, on 24 June 2011 - 12:58 PM, said:
I am new to Java but I know how Javascript works. Can somebody please explain to me how to use methods in Java like functions in Javascript. What is the difference between Methods and Classes in Java? Where and when should I implement them? Sorry if this is already posted or shouldn't be asked here. I can't find a clear example I can understand online. Thanks again!
So,
Methods are like function and you can make one like this :
public void testMethod(){
}
The method comes in the class.
Like this:
public class test {
public void testMethod(){
//Here your code
System.out.println("This is a test");
}
public static void main(String[] args){
testMethod();
}
}
Hope this helps a bit
#4
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:06 PM
Yes that helps out a lot. So how do I call a method like a function in Javascript? Thanks so much!
#5
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:07 PM
#6
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:15 PM
cornetto456, on 24 June 2011 - 04:02 PM, said:
public class test {
public void testMethod(){
//Here your code
System.out.println("This is a test");
}
public static void main(String[] args){
testMethod();
}
}
Hope this helps a bit
Not really, this code won't even compile.
As I mentionned you need an instance of a class to invoke a method in that class. That should work
public class test {
public void testMethod(){
System.out.println("This is a test");
}
public static void main(String[] args){
test t = new test();
t.testMethod();
}
}
#7
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:17 PM
pbl, on 24 June 2011 - 01:15 PM, said:
cornetto456, on 24 June 2011 - 04:02 PM, said:
public class test {
public void testMethod(){
//Here your code
System.out.println("This is a test");
}
public static void main(String[] args){
testMethod();
}
}
Hope this helps a bit
Not really, this code won't even compile.
As I mentionned you need an instance of a class to invoke a method in that class. That should work
public class test {
public void testMethod(){
System.out.println("This is a test");
}
public static void main(String[] args){
test t = new test();
t.testMethod();
}
}
Sorry for the incovienince :S
Maybe need to learn a bit more
#8
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:18 PM
unless the method is declared static
public class test {
static public void testMethod(){
System.out.println("This is a test");
}
public static void main(String[] args){
testMethod();
}
}
#9
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:24 PM
Thanks a lot guys. You guys are awesome!
#10
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:30 PM
One more question. What would be the best way to do an if statement that called the different methods in Java.
If (x == 1){
//DO METHOD 1
}Else{
//DO METHOD 2
I am creating an address book for school and have to have 4 options which let the user input a new entry with all the fields, show all of the entries, and quit the program. So I am going to give the users an option to choose from at first and than depending on their choice I will run a certain method. Sound Good?
Thanks Again!
If (x == 1){
//DO METHOD 1
}Else{
//DO METHOD 2
I am creating an address book for school and have to have 4 options which let the user input a new entry with all the fields, show all of the entries, and quit the program. So I am going to give the users an option to choose from at first and than depending on their choice I will run a certain method. Sound Good?
Thanks Again!
#11
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 01:33 PM
if (x == 1){
method1();
}else{
method2();
}
Java is case sensitive so it is if/else not If/Else
#12
Re: How to use methods in Java like functions in JavaScript
Posted 24 June 2011 - 06:33 PM
Check out the Oracle tutorial on methods. It covers some OOP as well, which will hopefully serve to give you a better idea of where/how to use methods. Hope this helps some.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|