This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static do this work ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Static do this work ???" Watch "Static do this work ???" New topic
Author

Static do this work ???

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 335
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
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
But the code should be return counter; and not return counter();
Is counter() another method defined in class a ?


http://www.skillassert.com


Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 335
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
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

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
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
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_


Mike Noel
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 335
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
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Static do this work ???
 
Similar Threads
array of objects confusion
Synchronizing thread woes!
I want to demonstrate an unsafe thread
Jsp expecting Static reference to execute java function
Size of Java Objects