File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes instance variables initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instance variables initialization" Watch "instance variables initialization" New topic
Author

instance variables initialization

srujana maredugu
Greenhorn

Joined: Jan 10, 2008
Posts: 8
public class As{
int i = 10;
int j;
char z= 1;
boolean b;
public static void main(String argv[]){
As a = new As();
a.amethod();
}
public void amethod(){
System.out.println(j);
System.out.println(b);
}}

In the above sample program,for char variable z was initialized with 1 without explicit cast.char is smaller than int,why it is not giving exception if we assign int value to char.

Regards,
Srujana.
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by srujana maredugu:

....for char variable z was initialized with 1 without explicit cast.char is smaller than int,why it is not giving exception if we assign int value to char.

Regards,
Srujana.


Hi Srujana,

A character is always a character no matter how you represent it.Look at the code below


Here variable a,b & c represent the same values.
In case of variable c cast is necessary because java performs all non-fraction arithmetic using four byte integers. The cast transforms it back to a two byte char type which we can assign directly.
[ February 12, 2008: Message edited by: Balasubramanian Chandrasekaran ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Originally posted by srujana maredugu:
In the above sample program,for char variable z was initialized with 1 without explicit cast.char is smaller than int,why it is not giving exception if we assign int value to char.

If you assign an integer literal that fits within the range the compiler will allow it; it's when the literal exceeds the range when the problems occur.

So for char you can assign any integer literal between 0 and 65535 (inclusive); any other number will cause a compile error.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sreekanth Munagala
Greenhorn

Joined: May 20, 2008
Posts: 4
Hi friends,
I have a doubt regarding instance variable initialization in java.
class Test
{
int a=10;//works fine
int a;
a=10; //compiler error
}
my question is
why the first version works and why not the second.
For me logically both should work.is there any reason behind it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38763
    
  23
Welcome to JavaRanch, Sreekanth Munagala.

If that really is the code you tried, you are declaring a twice. That is the problem.
[ May 20, 2008: Message edited by: Campbell Ritchie ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

I think Sreekanth means that it also will not work if the first declaration is omitted.

The reason is quite simple; you can't execute statements (such as a = 10) inside a class. You will need either a method, constructor or static / initializer block:

The difference between these two blocks is when they are called. Static blocks are called when the class is loaded, in order from top to bottom. Initializer blocks are called when the instance is created, from top to bottom, just before the constructor.
Sreekanth Munagala
Greenhorn

Joined: May 20, 2008
Posts: 4
Thankyou very much Rob and Campbell.
I got the answer for my query.
Rob,Initializer block works fine but,we can't use static block to initialize non-static variables.
Sreekanth Munagala
Greenhorn

Joined: May 20, 2008
Posts: 4
Rob,
Can we generalize like this-'A processing statement should be either in a method ,construcutor , initializer block or a static block.
Sreekanth Munagala
Greenhorn

Joined: May 20, 2008
Posts: 4
Does the compiler treat differently when we put a=0 outside the initializer block?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Originally posted by Sreekanth Munagala:
but we can't use static block to initialize non-static variables.

Correct, my mistake. It was just some quick coding to show you how a static initializer works. It has to oblige to all the rules for static methods though, so no instance variables / methods available without an actual instance.

Originally posted by Sreekanth Munagala:
Rob,
Can we generalize like this-'A processing statement should be either in a method ,construcutor , initializer block or a static block.

Correct, for any statement. That includes assignment and control structures. Only declarations can be used outside those four. Outside of a class nothing is allowed.

Originally posted by Sreekanth Munagala:
Does the compiler treat differently when we put a=0 outside the initializer block?

There may be very small differences between initializing a in an initializer block or directly when declaring (int a = 0) but the order is the same.
imaya Munusamy
Greenhorn

Joined: May 19, 2008
Posts: 20
Originally posted by srujana maredugu:
public class As{
int i = 10;
int j;
char z= 1;
boolean b;
public static void main(String argv[]){
As a = new As();
a.amethod();
}
public void amethod(){
System.out.println(j);
System.out.println(b);
}}

In the above sample program,for char variable z was initialized with 1 without explicit cast.char is smaller than int,why it is not giving exception if we assign int value to char.

Regards,
Srujana.


hi Srujana,

char z=1 -> it will set the value during compile time itself. compiler will check the range and set value. if the value is above the range of character it will give compiler error.

int i=1;
char z= (char) i; // need explicit casting because this will set value during runtime.
John M Morrison
Greenhorn

Joined: Jul 25, 2005
Posts: 21
You should initialize instance variables in the constructor(s) or the static block if they are static. This is better and cleaner style. I strongly discourage my students from doing stuff like this



Yuck. Create proper a proper static block (if needed) and proper constructors. This makes your intent as a class designer explicit.

JMM
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"imaya M",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance variables initialization