11 Replies - 856 Views - Last Post: 24 June 2011 - 06:33 PM Rate Topic: -----

#1 staley412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-February 11

How to use methods in Java like functions in JavaScript

Posted 24 June 2011 - 12:58 PM

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!
Is This A Good Question/Topic? 0
  • +

Replies To: How to use methods in Java like functions in JavaScript

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: How to use methods in Java like functions in JavaScript

Posted 24 June 2011 - 01:02 PM

View Poststaley412, 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 ;)
Was This Post Helpful? 0
  • +
  • -

#4 staley412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-February 11

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!
Was This Post Helpful? 0
  • +
  • -

#5 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: How to use methods in Java like functions in JavaScript

Posted 24 June 2011 - 01:07 PM

View Poststaley412, on 24 June 2011 - 01:06 PM, said:

Yes that helps out a lot. So how do I call a method like a function in Javascript? Thanks so much!

My post give's an example :)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to use methods in Java like functions in JavaScript

Posted 24 June 2011 - 01:15 PM

View Postcornetto456, 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();
   }
}


Was This Post Helpful? 2
  • +
  • -

#7 cornetto456  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 438
  • Joined: 03-January 11

Re: How to use methods in Java like functions in JavaScript

Posted 24 June 2011 - 01:17 PM

View Postpbl, on 24 June 2011 - 01:15 PM, said:

View Postcornetto456, 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();
   }
}


>.< , Damit :(

Sorry for the incovienince :S


Maybe need to learn a bit more ;)
Was This Post Helpful? -1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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();
   }
}


Was This Post Helpful? 2
  • +
  • -

#9 staley412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-February 11

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!
Was This Post Helpful? 0
  • +
  • -

#10 staley412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-February 11

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!
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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
Was This Post Helpful? 1
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

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. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1