Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Question problem

 
Neelima Vemu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried compiling?
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is also a good idea to not disable ubb codes, so that the code tags will actually work.

Henry
 
Ravinderjit Singh
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can use the code.



Have a look on this jsl
 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neelima please Quote Your Sources when you post a question...
 
Neelima Vemu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic