aspose file tools*
The moose likes Beginning Java and the fly likes  private static Test monitor = new Test(); -- Static + Private Object Referenc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " private static Test monitor = new Test(); -- Static + Private Object Referenc" Watch " private static Test monitor = new Test(); -- Static + Private Object Referenc" New topic
Author

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

vivek a naik
Greenhorn

Joined: May 15, 2008
Posts: 9


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

Joined: Apr 14, 2004
Posts: 10336

What is the question here?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
vivek a naik
Greenhorn

Joined: May 15, 2008
Posts: 9
Can we write...

private static Test monitor = new Test();
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

If Test has a constructor that does not take any arguments, then yes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vivek a naik
Greenhorn

Joined: May 15, 2008
Posts: 9
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
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jitendra Jha
Ranch Hand

Joined: Jan 28, 2007
Posts: 91
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 ]

Jitendra
SCJP1.5
SCWCD1.5
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
. . . and as Jitendra Jha has already hinted, you mustn't say public private protected or static inside a method.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Welcome Campee!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
Oh, thank you. Just noticing how helpful you were being.
vivek a naik
Greenhorn

Joined: May 15, 2008
Posts: 9
Thnaks...

i should not write static local object variable outside class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Not "outside class", but "inside a method."
Shan Priya
Greenhorn

Joined: May 17, 2008
Posts: 3
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: May 17, 2008
Posts: 3
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
When I said "access the same way," I meant from inside the class. From outside the class you write ClassName.staticFieldName.
Shan Priya
Greenhorn

Joined: May 17, 2008
Posts: 3
Thanks a lot!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Originally posted by Shan Priya:
Thanks a lot!
You're welcome. It's always nice to find we have been useful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private static Test monitor = new Test(); -- Static + Private Object Referenc
 
Similar Threads
Problems with importing from another source code
clone() used on array and object
about accessing and importing from directory
Javabeat query
struct in java