• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static do this work ???  RSS feed

 
Frank Jacobsen
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the code should be return counter; and not return counter();
Is counter() another method defined in class a ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Frank Jacobsen
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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_
 
Frank Jacobsen
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!