aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes // simple Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "// simple Question" Watch "// simple Question" New topic
Author

// simple Question

chao-long liao
Ranch Hand

Joined: Jul 29, 2001
Posts: 78
1 class Red {
2 public int a;
3 public static int b;
4 public static void main (String[] in) {
5 Red r1 = new Red();
6 Red r2 = new Red();
7 r1.a++;
8 r1.b++;
9 System.out.println
10 (r1.a+", "+r1.a+", "+r2.a+", "+r2.b);
11 }
12 }
Somebody please tell me what's the order of the code.ex: 2->3->4->5->2->3
I am confused.Thanks.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by chao-long liao:
1 class Red {
2 public int a;
3 public static int b;
4 public static void main (String[] in) {
5 Red r1 = new Red();
6 Red r2 = new Red();
7 r1.a++;
8 r1.b++;
9 System.out.println
10 (r1.a+", "+r1.a+", "+r2.a+", "+r2.b);
11 }
12 }
Somebody please tell me what's the order of the code.ex: 2->3->4->5->2->3
I am confused.Thanks.

I'm glad that you posted this question, because I just noticed a code bug. Fortunatly, it doesn't change the answer. The corrected code is as follows.

The purpose of the question is to demonstrate that both instances of class Red share a single copy of field b. The output is 1, 1, 0, 1. Fortunately the old version prints the same result.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
Hi,
Sorry to bother with beginner questions... but, this code of yours brought me two confusions:
1 - your class didn't declare any constructor. Is it always acceptable to invoke new anyClass(), assuming that anyClass doesn't have a constructor?
2 - the code assumes both integer were initialized with 0 value. Does java always initialize variables for me? If so, with wich values are a boolean and a char initialized?

I'm starting to study Java now, and got a long jorney then ... it's just that I have my brain 'empty' by now, and I'm gradually adding the java 'rules' to it. But I need to be shure of them not to write anything invalid here, right?
Many thanks!
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
- the code assumes both integer were initialized with 0 value. Does java always initialize variables for me? If so, with wich values are a boolean and a char initialized?

variable a is an instance variable and b is a static variable in the above code.

Static variables are initialized to default values when the class is loaded,if they are not explicitly initialized.

Instance variables are initialized to default values when the class is instantiated,if they are not explicitly initialized.
Local variables have to be initialized explicitly.if they are not initialized when they are instantiated at method invocation,compiler reports an error.
if a boolean variable is declared as static/instance variable then it is set to its default value ie false
Here is a list of datatypes and their defalut values
datatype default value
boolean false
char '\u0000'
Integer(byte,short,int,long) 0
Floating-point(float,double) +0.0F/0.0D
Object reference null
-bani
Libania Paes
Greenhorn

Joined: Aug 02, 2002
Posts: 14
The output is 1, 1, 0, 1.

Wait a minute... Why the last value is 1? Is it because de variable is declared static?
If so, static variables maintain the values assumed to them? They are "shared" by all the objects that call them?
Thanks!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

your class didn't declare any constructor. Is it always acceptable to invoke new anyClass(), assuming that anyClass doesn't have a constructor?

Yes. If you do not declare any constructors, a default constructor with no arguments shall be provided for you. However, if you declare a constructor with a different argument, you must also explicitly declare a no args constructor if you want to use one.

static variables maintain the values assumed to them? They are "shared" by all the objects that call them?

Yes. Static variables are shared by all instances of that class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: // simple Question