Solved

static objects

Posted on 2003-11-24
5
290 Views
Last Modified: 2010-04-16
What are the advantages/disadvantages of making class objects as static ? Give examples in case of either case.
0
Comment
Question by:NeerajVij
5 Comments
 
LVL 2

Accepted Solution

by:
DidierD earned 34 total points
ID: 9816169
0
 
LVL 3

Assisted Solution

by:monkesdb
monkesdb earned 33 total points
ID: 9816228
when you declare an object as static it is the same / shared through all instances of that class.

public class A
{
    public int val;
}

public class B
{
    public static int val = 0; // i think you need to initialise it here, i might be wrong, you could leave this out.
}

public class Main
{
    public static int main(String args[])
    {
        A a1 = new A();
        A a2 = new A();
        a1.val = 1;
        a2.val = 5;
        System.out.println( a1.val + ", " + a2.val );

        B b1 = new B();
        B b2 = new B();
        b1.val = 1;
        b2.val = 5;
        System.out.println( b1.val + ", " + b2.val );

    }
}

when you run this code you will see that in B the variable val is the same in both instances now this is useful and in some cases essential. that, however, is something you would have to determine per problem.
0
 

Assisted Solution

by:CapitalJ
CapitalJ earned 33 total points
ID: 9816394
static means that it is a CLASS variable, instead of an INSTANCE vairable.

public class Test1 {

  public static String str1 = "This is a STATIC variable";
  public String str2 = "This is a non-static variable";

}

public class Test2 {

  public static void main(String[] args) {

    System.out.println(Test1.str1);
    //I'm NOT instantiating an Object of type Test1,
    //I reference the CLASS

    //I can NOT do the following, it won't compile:
    System.out.println(Test1.str2); // WRONG!!!

    //I have to do this:
    Test1 t1 = new Test1();
    System.out.println(t1.str2);

    //I CAN also reference static variables from the OBJECT like:
    System.out.println(t1.str1);
   
  }

}

static variables is used when you want to have acces to them via the CLASS, NOT the OBJECT.

You can reference static variables via the CLASS OR the INSTANCE of that class.
You can ONLY reference non-static variables from an INSTANCE of that class.

Hope this helps
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question