Results 1 to 7 of 7

Thread: Java example for static members

  1. #1
    Join Date
    Dec 2009
    Posts
    68

    Java example for static members

    Hello Friends,

    I am totally confused in between the static function concept. The static function syntax is still not cleared for me. I don't know how solve this confusion.
    If you have sound knowledge about the java static members and methods then please let me know that with any suitable example. Your help would be greatly appreciable.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java example for static members

    Please refer the below java example for static members:
    public class StaticMemberExampleDM
    {

    public static void main(String[] arg)
    {

    ObjectCounter objectDm1 = new ObjectCounter();

    System.out.print(objectDM1.getNumberOfObjects());


    ObjectCounter objectDM2 = new ObjectCounter();

    System.out.print(objectDM2.getNumberOfObjects());

    }

    }

    class ObjectCounterDM
    {

    static int counterDM=0;

    public ObjectCounter()
    {

    counterDM++;

    }

    public int getNumberOfObjects(){

    return counterDM;

    }

    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Java example for static members

    Hi,

    The static method of is also the static members of java. The value of the static methods and members are global i.e it's value doesn't differ form class to class. See below example of the static methods:
    public class StaticExample
    {

    public static void main(String[] ars)
    {

    int NumResult = MathUtility.add(2,3);

    System.out.print("(2+3) is : " + Numresult);

    }

    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Java example for static members

    See something below can help you:
    public class DMOStaticVariable{
    static int Ins;

    StaticVar()
    {
    Ins++;
    }
    public static void main(String args[])

    {
    StaticVar DMsv1 = new StaticVar();

    System.out.println("No. of instances for sv1 : " + DMsv1.Ins);

    StaticVariable DMsv2 = new StaticVariable();
    System.out.println("instances for sv1 : " + DMsv1.Ins);
    System.out.println("instances for st2 : " + DMsv1.Ins);

    StaticVariable DMsv3 = new StaticVariable();
    System.out.println("nstances for sv1 : " + DMsv1.Ins);
    System.out.println("instances for sv2 : " + DMsv1.Ins);
    System.out.println("instances for sv3 : " + DMsv1.Ins);

    }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Java example for static members

    The Java programming language has support to static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in "ClassName.methodName(args)". A common use for static methods is to access static fields.
    For example, we could add a static method to the Cycle class to access the numberOfCycles static field:
    public static int getNumberOfCycles()
    {
    return numberOfCycles;
    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Java example for static members

    I have following program, please study carefully this example. I hope it will depict you how to use and access the static members in java programming.
    public class AccessStaticMethodDemo{
    int i;
    static int j;
    public static void staticMethodDemo()
    {
    System.out.print("Access a static method this way");

    }
    public void nonStaticMethodDemo(){
    P=200;
    Q=2000;
    System.out.println("Don't try to access a method which is non static method");
    }
    public static void main(String[] args) {
    P=200;

    staticMethodDemo();
    }
    }

  7. #7
    Join Date
    Feb 2009
    Posts
    96

    Re: Java example for static members

    I'll explain it in easy terms-- looking at code isn't always the best.

    static means that it is represented as the same variable through out all the classes. Like if you wanted a tax rate of something to stay the same in all the classes you would write something like static double rate = .07; this would stay the same in all the classes as long as you declare it before any method in a class like:
    class productByState
    {
    your static would go here
    then methods
    }

    think of it like static in a house and you touch something you pass that electric current from yourself to a wall or something and the current never changes.

Similar Threads

  1. What is static method in java?
    By Vaibhav S in forum Software Development
    Replies: 5
    Last Post: 25-09-2011, 09:40 AM
  2. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  3. What is static data members of class?
    By Jaiwanti in forum Software Development
    Replies: 3
    Last Post: 20-11-2009, 10:48 AM
  4. How to initialize static members in template classes
    By Aienstaien in forum Software Development
    Replies: 3
    Last Post: 05-05-2009, 06:36 PM
  5. When to Declare a static method in JAVA
    By Aamin in forum Software Development
    Replies: 2
    Last Post: 04-02-2009, 07:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,253,690.66053 seconds with 17 queries