?
Solved

static objects

Posted on 2003-11-24
5
Medium Priority
?
297 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 136 total points
ID: 9816169
0
 
LVL 3

Assisted Solution

by:monkesdb
monkesdb earned 132 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 132 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
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.
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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