Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Understanding Static Methods  (Read 779 times)
0 Members and 1 Guest are viewing this topic.
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« on: August 08, 2009, 11:34:39 AM »

In Static Methods you dont have to create a an instance of a class to use the methods. If a method with the static keyword, the method can be called without first creating an instance of the class, this is becasue static methods are called from classes, not from objects.

The main method of a java app must be declared with the static keyword, this is how we know....
That's becasue when you start a java program by using the java command from cmd, java doesnt create an instance of the application class, instead it bascially makes it the static main method.

Many Java Programs are made up of static methods, however this tutorial is only for you guys to understan them better, most realistic programs require that you create one or more objects that the program uses to exacute.

I hope the guide has helped you understand the basics of static methods, and how you use them.

Ikram
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #1 on: August 08, 2009, 12:21:36 PM »

To give an example here's a static method

Code
GeSHi (java):
  1. public class StaticTest {
  2. public static void main(String[] args){
  3. System.out.println(new StaticTest().staticMethod("_ikram"));
  4. }
  5.  
  6. public static String staticMethod(String name){
  7. String s = "Hello " + name + " !";
  8. return s;
  9. }
  10. }
  11.  
Created by GeSHI 1.0.7.20

output
Hello _ikram !
« Last Edit: August 08, 2009, 12:41:33 PM by _ikram » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
_ikram
Member
*

Reputation: 3
Offline Offline
Posts: 65
Referrals: 0

Awards
« Reply #2 on: August 08, 2009, 12:41:58 PM »

Thanks HP for the example.
Logged

RuneScape Developers community: www.rspsnetwork.com
Hextion Private Server: www.hextion.com
jurka
Staff
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #3 on: August 08, 2009, 03:06:33 PM »

Many of the newer programming languages has removed static keyword like in Scala. Use static keyword carefully, not just every method must be static, think before using. It may come bad habit for OO world.
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #4 on: August 08, 2009, 03:47:12 PM »

Many of the newer programming languages has removed static keyword like in Scala. Use static keyword carefully, not just every method must be static, think before using. It may come bad habit for OO world.

Any examples when to use statics and when not or why not?
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
jurka
Staff
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #5 on: August 08, 2009, 04:10:21 PM »

Any examples when to use statics and when not or why not?
Ok as i might mess up the point of explaining why to use static with care, ill give better a link http://beust.com/weblog/archives/000174.html.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here