• 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

// simple Question

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

- 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic