Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private static Test monitor = new Test(); -- Static + Private Object Referenc

 
vivek a naik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


check code with explaination here .. Here clone, presumably from Bruce Eckel Thinking in Java.

[edit]Added code tags, and altered displayed part of URL tag. CR[/edit]
[ May 26, 2008: Message edited by: Campbell Ritchie ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the question here?
 
vivek a naik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we write...

private static Test monitor = new Test();
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Test has a constructor that does not take any arguments, then yes.
 
vivek a naik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.

you are right.it will compile.

but,how can an Object Reference be a "Static"..??

Could you explain this by an example??


class Test
{
Test()
{
System.out.println("Static");
}

void display()
{
System.out.println("disp");
}
}

public class MainClass
{
Test tt=new Test();
private static Test t1=new Test();
Test t2=new Test();
t1.display();
tt.display();
}




//it gives compile time error....

Program Files\Xinox Software\JCreatorV4\MyProjects\MainClass\src\MainClass.java:20: <identifier> expected
tt.display();
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but,how can an Object Reference be a "Static"..??

Could you explain this by an example??


What does it matter whether it is an object reference or not? It is a class variable which is such because it is static.

In your second example, you are trying to declare a local variable as static, and there is no such a thing as a local static variable.

Henry
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your syntax is wrong
you are making a method call from a global context(from outside a method)
if you make the following changes in the MainClass ----

public class MainClass
{
public static void main(String str[])
{
Test tt=new Test();
private static Test t1=new Test();
Test t2=new Test();
t1.display();
tt.display();
}
}


Then the compiler gives you the error regarding the use of private static
[ May 26, 2008: Message edited by: Jitendra Jha ]
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Vivek,

Welcome to JavaRanch.
Please take one more look at your 'mainClass'. Where did you put your statements? And always copy and paste the code from the original location.
Always quote your code with 'CODE' tag so that others can understand easily.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal, thank you for noticing about code tags and copy-and-paste. I shall edit the original post to add code tags.

Vivek: If you quote material from a book or website, please make sure it is obvious to whoever reads the posting where you got it from.
[ May 26, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and as Jitendra Jha has already hinted, you mustn't say public private protected or static inside a method.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Campee!
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishal Pandya:
Welcome Campee!
Thank you.

I presume you mean as bartender, since I was a member before you.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Thank you.

I presume you mean as bartender, since I was a member before you.


That 'Welcome' was for this.

Vishal, thank you for noticing about code tags and copy-and-paste. I shall edit the original post to add code tags.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, thank you. Just noticing how helpful you were being.
 
vivek a naik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks...

i should not write static local object variable outside class.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not "outside class", but "inside a method."
 
Shan Priya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand how you can create an object reference and make it static. The whole idea of static variables is to treat them as global variables and use them using Class name i.e Classname.static variable name.

static Test monitor = new Test();
In the above example, why do we need to create a static object reference. Can static be applied for object references. Can some body explain clearly a good desc of when you can make object references static and why do we need one? If we create a static object ref, then can we use this keyword to refer to the current object.

I'm just trying out to get a good understanding.

Thanks
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shan Priya: Welcome to JavaRanch

Of course you can have a static reference to an object. What it means is that the reference belongs to the class and every object of that class uses the same reference, shared.

Imagine you have a class which uses resources put into a Map. If the same resources are always inthe Map, then is can be static, and used (or altered) by every object of that class. That way all instances have access to the same resources at any time.
 
Shan Priya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the description. Can you please suggest some websites or books where i can find examples for static object references? I just want to clear out some other questions once i see and run those examples.

questions are:
1. How to make class object access the static object references if i want to add/modify or get the resources from static object i.e Map?
2. Where to declare the static object reference i.e outside the main() or inside()? Any rules for this.

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shan Priya:
Thanks for the description. Can you please suggest some websites or books where i can find examples for static object references? I just want to clear out some other questions once i see and run those examples.

questions are:
1. How to make class object access the static object references if i want to add/modify or get the resources from static object i.e Map?
2. Where to declare the static object reference i.e outside the main() or inside()? Any rules for this.

Thanks.
You access a static reference the same way you access an instance reference.
You declare static references in the same place you declare instance references, but there are some conventions that static declarations are written first.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I said "access the same way," I meant from inside the class. From outside the class you write ClassName.staticFieldName.
 
Shan Priya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shan Priya:
Thanks a lot!
You're welcome. It's always nice to find we have been useful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic