Page 1 of 1

Introduction to Designing Classes II Intro to designing class with static methods. Rate Topic: -----

#1 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 30 July 2008 - 04:22 PM

*
POPULAR

Well, this is me second tutorial on designing classes with static methods. I'm gonna start you off with a very basic example: the Math class.

This class has methods that do mathematical calculations...I know...duh. Well, the methods of the Math class are static, meaning you don't have to instantiate an object to call them. You can simply call them like this.

Math.METHOD(parameters_used);


You DON'T have to do this:

Math mathObject = new Math();

mathObject.METHOD();


You don't HAVE to do it that way...but you can, but no one I know does that. :)/>


Well, we'll make a method that does a very simple calculation. We'll make an addition() method.

class OurMath
{
    public static int addition(int a, int b )
    {
        int sum = a + b;

        return sum;
    }
}


Notice how we declare our int return type for the method. This makes it so we can assign it to an int variable in a main method or something.

Also notice the int variables in the parentheses. These are called 'parameters'. These are what's going to be included in the method when it's called.

It's going to be called like this.

int number = OurMath.addition(1, 3);


Well, anyone know what number is going to equal after that? That's right! Four!

You HAVE to call the method with 2 parameters. If you call it any other way, you're going to going to get a compile error saying something along these lines. cannot find symbol: method addition(int)


static Class Variables

Well, back again to the Math class. It has 2 double variables, E, and PI.

class Math
{
    static double E = 2.718...;
    static double PI = 3.141596535...;
}


This is how we declare static (AKA "class") variables. Let's go back to our class...

class OurMath
{
    static int five = 5;

    public static int addition(int a, int b )
    {
        int sum = a + b;

        return sum;
    }
}


Well, now we can access our five variable through doing this.

int ourVariable = OurMath.five;


The main reason of using static is to make non "object"-ive data or methods.

Hope this tutorial helped anyone that needed it! :D

Is This A Good Question/Topic? 5
  • +

Replies To: Introduction to Designing Classes II

#2 Djanvk  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 14-December 08

Posted 24 February 2009 - 05:18 AM

Just ran across this tutorial and it was exactly what I was looking for. Thanks for posting this.
Was This Post Helpful? 0
  • +
  • -

#3 bvarghese2155  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Posted 13 August 2012 - 01:18 PM

So can someone explain to me what static method is? Can you have one in a class? Can objects refer to them?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,155
  • Joined: 12-June 08

Posted 13 August 2012 - 01:24 PM

@bvarghese2155 Icon

From the horse's mouth on static variables and static methods...

Quote

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

http://docs.oracle.c.../classvars.html
Was This Post Helpful? 0
  • +
  • -

#5 novellof  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 24-May 14

Posted 27 May 2014 - 01:57 AM

class OurMath
{
static int five = 5;

public static int addition(int a, int B)/>
{
int sum = a + b;

return sum;
}
}

this is an error correct?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,840
  • Joined: 27-December 08

Posted 28 May 2014 - 12:58 PM

It's actually a bug with the forum software. I've made the edit to the tutorial.

Btw- please remember to use code tags: :code:.
Was This Post Helpful? 0
  • +
  • -

#7 novellof  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 24-May 14

Posted 28 May 2014 - 02:22 PM

Thanks i know it was a negligible error but someone who is new might be confused if they were looking through it...i know i probably would.

Anyway I am new here and I would like to know how you were able to edit your post I can't find that out for some reason...If you could pm me how to do it or just post a reply here, don't want to muddy up the topic.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,840
  • Joined: 27-December 08

Posted 28 May 2014 - 02:36 PM

I'm an admin and can edit anyone's post. Members can only edit posts once they've reached a certain post count.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1