File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Question problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Question problem" Watch "Mock Question problem" New topic
Author

Mock Question problem

Neelima Vemu
Greenhorn

Joined: Jan 16, 2010
Posts: 12
Hi ,
In the following code is it right to use
[code] static{
i+=10;} [/code]

Can anyone please explain?


[code]class TestQuestion {
static int i = 10;
static {
i+= 10;
}
public static void main(String s[]) {
System.out.println("i:" +i);
}
static {
i+=12;
}
}[/code]
Rajesh Shinde
Greenhorn

Joined: Jan 29, 2010
Posts: 11
Tried compiling?


scjp6 processing.....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

It is also a good idea to not disable ubb codes, so that the code tags will actually work.

Henry


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

Joined: Jan 13, 2010
Posts: 78
Yes, you can use the code.



Have a look on this jsl
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
[quote=Neelima Vemu]Hi ,
In the following code is it right to use
[code] static{
i+=10;} [/code]

Can anyone please explain?


[code]class TestQuestion {
static int i = 10;
static {
i+= 10;
}
public static void main(String s[]) {
System.out.println("i:" +i);
}
static {
i+=12;
}
}[/code][/quote]


Yes you can use. Static variables are also called class variables (shared by all the objects).
looking at your code. When the code is run first the initialization of static variable takes place first (i=10) , after that the static block is invoked which add the value of it to itself (i=i+10; // i = 20).

Static blocks are always called once and only once, when the class is run first time.
Neelima Vemu
Greenhorn

Joined: Jan 16, 2010
Posts: 12
Hi All,

Thanks for the reply! Yes I tried compiling and of course the output was 32. But before compiling I thought its not per the syntax to use the code enlisted in the static block.

So its a legal declaration?Since I haven't been through such declarations before.

Thanks again
Neelima
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

Neelima please Quote Your Sources when you post a question...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Neelima Vemu
Greenhorn

Joined: Jan 16, 2010
Posts: 12
Ankit Garg wrote:Neelima please Quote Your Sources when you post a question...


Hi Ankit,

I wish I can do that. But its from a random mock test paper that I found in my friend's collection. This is the question exactly.The one that I had posted.And was actually under the confusion if I could do the declaration like that.


I have another doubt from the K&B SCJP1.5 guide Page 194-195


In the above code how is title an instance reference variable? Please can anyone explain?

Neelima
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
I have another doubt from the K&B SCJP1.5 guide Page 194-195


In the above code how is title an instance reference variable? Please can anyone explain?

Neelima

The code is straight forward.
is an instance variable of the class Book. Instance variables are invoked via class objects.
Since your variable is private, hence the only way to access is via the method .
In line number 8 you are creating an instance/object of class Book (named as "b", each object of this class will have a variable of type string named title and same method named getTitle()) and than in you are calling the method getTitle() using the class object "b". The method returns you the title.

don't confuse yourself with saying it "instance reference variable", "just call every variable that is declared within class and outside any method as "instance variable of the class" ... if the variable is declared using the keyword "static" than it is called "Class Variable".

In case the code would have been like this:

it means that this variable will be shared among all the objects of the class, its a common variable to all objects. hence you call it using the class name, not object.


hope you get something out of this.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

Neelima Vemu wrote:I wish I can do that. But its from a random mock test paper that I found in my friend's collection.

It doesn't work that way, if you want to discuss a question, you should know the source. There must be some place in the web page (or any other document from which you are reading) where the name of the mock exam is mentioned. Why don't you give online mocks (see ScjpMockTests) instead of your friend's collection so that you'll know the exact source. The mock exams available online are ought to be more in quantity than your friend's collection...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Question problem