Solved

static objects

Posted on 2003-11-24
5
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

717 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