Forums Register Login

Static do this work ???

+Pie Number of slices to send: Send
public class a {
static int counter;

public static int getCounter() {
counter++;
return counter();

}

}

I know this class is not destroyed, is somewhere in my JVM, and the counter is updated from another class, i think is on my session, but can i do this, whitout getting this object from the session:

public class b {

public void dostuff() {
return a.counter();
/* Do this work, im not making any instance off class a or do i have to
make an intance, or do i have to get the right object from session to get the counter ???
Or can i make a intance off class b, to get the counter ???
*/
}

}

Frank
+Pie Number of slices to send: Send
But the code should be return counter; and not return counter();
Is counter() another method defined in class a ?
+Pie Number of slices to send: Send
You do not have to create an instance of a class to call a static method on the class. In fact, it's bad style to call static methods on instances.

Your code does have some mistakes, however. First of all, method getCounter in class a should return "counter" and not "counter()". Second, in method dostuff in class b, you want to return "a.getCounter()" and not "a.counter()".

I don't know what you mean by "session". Are you running this in a servlet container and are you talking about the HttpSession?
+Pie Number of slices to send: Send
public class a {
static int counter;

public static int getCounter() {
counter++;
return counter;

}

}

I know this class is not destroyed, is somewhere in my JVM, and the counter is updated from another class, i think is on my session, but can i do this, whitout getting this object from the session:

public class b {

public void dostuff() {
return a.counter;
/* Do this work, im not making any instance off class a or do i have to
make an intance, or do i have to get the right object from session to get the counter ???
Or can i make a intance off class b, to get the counter ???
*/
}

}

Yes its two diffrent classes , and class b only knows that class a exist in the java vm, can I from class b calls a.counter, do class b knows where to find static int counter in the JVM ???

Yes i mean a HTTPServlet !

Frank
+Pie Number of slices to send: Send
 

Originally posted by Frank Jacobsen:
Yes its two diffrent classes , and class b only knows that class a exist in the java vm, can I from class b calls a.counter, do class b knows where to find static int counter in the JVM ???



Static members in a class are members that exist only once, for all instances in the class. So if you would have multiple instances of class a, all those instances would share the same variable named 'counter'.

Yes, you can access a.counter from class b, the variable is accessible (in your example, you didn't use any access modifier, so a.counter is visible to all classes in the same package as class a).

Have a look at this: Understanding Instance and Class Members
+Pie Number of slices to send: Send
Keep in mind that the static variable "counter" has default access. This means it can only be accessed from classes in the same file or in the same package. In your b class you access this varialbe directly with "return a.counter;". This will fail if the b class is in another package.

The a class provides a public getCounter() method. The b class should use this to access the value of counter. This will work no matter which package b is in.

_M_
+Pie Number of slices to send: Send
counter is static member shared along all instance of the object "a" then i asume this will work, i will first run a and then b:

package a;

public class a {
public static int counter;

public static int getCounter() {
counter++;
return counter;

}

public static void main(String [ ] args)
{
a1.getCounter();
a1.getCounter();

}



}

package b;

public class b {


public static void main(String [ ] args)
{
a.getCounter(); // im not making a new instance of a
System.out.println(a.getConter); // This will give me the result 2

}

}

Is this correct ???

Frank
+Pie Number of slices to send: Send
Not exactly

package a;

public class a {
public static int counter;

public static int getCounter() {
counter++;
return counter;

}

public static void main(String [ ] args)
{
a1.getCounter();
a1.getCounter();

}



In this case if you try to compile the program, the compiler will have no idea what a1 is. To properly call a static method you should call ClassName.methodName(), so in your example you should call a.getCounter(). However since your main method is in the same class as your getCounter() method you could alternatively just call getCounter().

package b;

public class b {


public static void main(String [ ] args)
{
a.getCounter(); // im not making a new instance of a
System.out.println(a.getConter); // This will give me the result 2

}

}


This won't work either for different reasons. Since you declared the Classes in seperate packages, you would have to import Class a to gain access to it, or else the compiler will complain that it doesn't know what variable 'a' means.
[ January 20, 2006: Message edited by: Garrett Rowe ]
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 844 times.
Similar Threads
Jsp expecting Static reference to execute java function
I want to demonstrate an unsafe thread
Synchronizing thread woes!
Size of Java Objects
array of objects confusion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:11:46.