• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Static Question which is worth to know

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class staticTest
{
static {
int x=10;
}
static int x; // Initialize a value and check what happens
static int y;
public static int getstatic(int x){
y=x++ + ++x + ++y;
return y;
}
public static void main(String[] args) {

// int x=0; // #10
System.out.println(" Passing -1 then " + getstatic(-1)); // #1
System.out.println(" Passing 0 then " + getstatic(0)); // #2
//System.out.println(" Passing -1 then " + getstatic(-1)); // #3
//System.out.println(" Passing -1 then " + getstatic(-1)); // #4

System.out.println(x); //#5
System.out.println(y); //#6

staticTest t1= new staticTest(); //#7
System.out.println(t1.x); //#8
System.out.println(t1.y++); //#9

}
}


// Guys Hope so u will enjoy....
// Don't be fooled seeing such kind of Questions.
// Don't be Hurry. We know things but if we are urgent to complete the Exam things goes wrong
// I was wrong during taking a mock exam after analying why am i wrong things came out like this.
// I hope this will even help u
// Correct me if i am wrong

// #1 if x = -1 then -1 + 1 + 1 = 0 i.e y is 1;
// #2 if x = 0 then 0 + 2 + 2 = 4 i.e y is 4
// #3 if x = -1 then -1 + 1 + 5 = 0 i.e y is 5;
// #4 if x = -1 then -1 + 1 + 6 = 0 i.e y is 6;

// #5 x is 0
// #6 y is 6

// Copy the line #7, #8, #9 above #1 and see the fun....

// Remove the comment at #10 and Enjoy

// Things to observer. x variable in getStatic method is local variable.
// Y is a class variable.
// Exam Creators will confuse us with same name which they have defined as member variable.


// I have learned a good lesson and wish to share with u .....................................

// Correct me if i am wrong anywhere............. I will learn.



Santhosh
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good One.

Asha
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one correction.

staticTest should be StaticTest.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing.
[ April 21, 2006: Message edited by: Aum Tao ]
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

// #1 if x = -1 then -1 + 1 + 1 = 0 i.e y is 1;



If i am right, the correct breakup is

if x = -1, then 0 + 0 + 1 = 1 and y is returned as 1.

Due to the pre increment operator ( ++x) x = -1 becomes 0 and hence the above result.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

// #1 if x = -1 then -1 + 1 + 1 = 0 i.e y is 1;



If i am right, the correct breakup is

if x = -1, then 0 + 0 + 1 = 1 and y is returned as 1.

Due to the pre increment operator ( ++x) x = -1 becomes 0 and hence the above result.
 
santhosh Gopalakrishna Bajja
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aum Tao:


If i am right, the correct breakup is

if x = -1, then 0 + 0 + 1 = 1 and y is returned as 1.

Due to the pre increment operator ( ++x) x = -1 becomes 0 and hence the above result.




#1. X=-1 Then

y = x++ + ++x + ++y
y = -1 + 1 + 1 Y= 1

Let's see how

y = x++ + ++x + ++y

x++ --> value of x. i.e x = x + 1; this is a statement and after this x is 0 but as this is post increment the increment vlaue will not be taken. the value taken in the expression is -1

++x --> Increment value of x. ie x= 0 + 1 = 1. Preincrement value is taken as a input.

++y --> member variable. by default assigned to 0 ie Preincrement hence input value is 1

.'. -1 + 1 + 1 = 1

y = 1;


santhosh
[ April 21, 2006: Message edited by: santhosh Gopalakrishna Bajja ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this program gives a compiler error, i know that there should be one public class in a file and it should match with the ame of the file.but are there any restrictions on using protected?
import java.util.*;

public class newtree extends newtreeset {

public static void main(String [] args) {
newtree t = new tree();
t.count();
}
}

protected class newtreeset {
void count() {

System.out.println("yes");
}

}
 
santhosh Gopalakrishna Bajja
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sirisha sirisha:
this program gives a compiler error, i know that there should be one public class in a file and it should match with the ame of the file.but are there any restrictions on using protected?
import java.util.*;

public class newtree extends newtreeset {

public static void main(String [] args) {
newtree t = new tree();
t.count();
}
}

protected class newtreeset {
void count() {

System.out.println("yes");
}

}




Good to accept that the program will not compile due to

1. " newtree t = new tree(); " Should be newtree t = new newtree();

2. classes can be either public, abstract,final or noaccess modifier. inner classes can be of any access modifier.



santhosh
[ April 21, 2006: Message edited by: santhosh Gopalakrishna Bajja ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good One
 
sirisha sirisha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this program gives a compiler error, i know that there should be one public class in a file and it should match with the ame of the file.but are there any restrictions on using protected?
import java.util.*;

public class newtree extends newtreeset {

public static void main(String [] args) {
newtree t = new tree();
t.count();
}
}

protected class newtreeset {
void count() {

System.out.println("yes");
}

}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sirisha,
A top level class can be either Abstract or Final or Public or a default class. We cannot have a top level class with protected or Private access nodifiers!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic