Win a copy of Rust Web Development this week in the Other Languages forum!

priya shende

Greenhorn
+ Follow
since Mar 17, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by priya shende

Thanks to all of you!!!




Priya shende
---------------
SCJP 1.5
13 years ago
Hi!
I passed my SCJP exam with 87% score today .I read only SCJP5's Kathy Sierra and Bert Bates and solved lot of mock exams.Thanks to all JavaRanchers.Thanks to everyone who helped me.




Priya Shende
--------------
SCJP 1.5
Hi!
I passed my SCJP exam with 87% score today . I completed my exam in almost 1 and half hour.I read only SCJP's Kathy Sierra and Bert Bates And solved lot of mock exams.Thanks to all JavaRanch.Thanks to everyone who helped me.




Priya Shende
--------------
SCJP(1.5)
13 years ago
Hi Arav,

Instance variable are initialized when the class is intantiated.When ever we create a object with new keyword constructor of that class is implicitly called.The compiler assigns default value to instance variable in a constructor.But we know that any constructor prior to excuting any code call's super class constructor implicitly with super(); statement.So super call constructor is called and its instance variable get default values.And agin if super class has its own super class its super super class constructor is called.Until you reach to object class.
Like in following example:

class Animal {
int x;

Animal(){
System.out.println("Animal class instance variable"+x);
}
}

class Horse1 extends Animal
{
int y;

Horse1(){//first calls super class const.i.e Animals class constructor
System.out.println("Horse1 class instance variable"+y); //after exucution of super class constructor initialize instance variable with default values
}
public static void main(String[] ar)
{

Horse1 d=new Horse1(); //calls horse class constructor
}
}

output
------------
Animal class instance variable 0
Horse1 class instance variable 0


Here first Horse1 class construtor is invoked which first call's Animal class constructor.Then Animal class constructor gets invoked then its completed means default value is assigned to Animal class instance variable.Then horse1 class constructor initialize instance variable of horse1 class and it is completed.In this way instance variable get their defalut values.


Priya
-----------
SCJP(prerparing).
Hi Mukhan,
To access instance variable and instance method we need an object of that class.And whenever we create a object,constructor of that class is called.Any constructor implicity calls Parent class constructor through super(); statement.Which in return calls to its superparent class.
For example if we inherit class Animal in class Horse the inheritance tree will be like:

Object-->Animal-->Horse;

class Animal {
Animal(){
//super();
}
}
class Horse extends Animal
{
Horse(){
super(); //calls super class constructor
}

public static void main(String[] ar)
{
Horse d=new Horse(); //calls constructor
}
}



So when ever you will make Horse class object it will call constructor of Horse class,which in return will call Animal class Constructor,whether you supply one or not compliler will implcitly add one statement like super(); to invoke animal class constructor.And Animal class constructor will call Object class constructor.
First Object class constructor completes that Animal class constructor completes and then atlast Horse class constructor completes.When the horse class constructor completes than only object is created.And with the help of that object we access instance variable and instance method.So we conclusion is to access a object we need instance of a class And to create instance we need constructor,and construtor implicitly class parent class constructor.
Therefore
That's because you cannot invoke an instance (in other words, nonstatic)
method (or access an instance variable) until after the super constructor has run.


Priya
---------
SCJP(preparing);
Hi,

Following lines are from K&B book,page no.128.(Constructor Chaining)


We know that constructors are invoked at runtime when you say new on some class
type as follows:
Horse h = new Horse();
But what really happens when you say new Horse() ?
(Assume Horse extends Animal and Animal extends Object.)
1. Horse constructor is invoked. Every constructor invokes the constructor
of its superclass with an (implicit) call to super(), unless the constructor
invokes an overloaded constructor of the same class (more on that in a
minute).
2. Animal constructor is invoked (Animal is the superclass of Horse).
3. Object constructor is invoked (Object is the ultimate superclass of all
classes, so class Animal extends Object even though you don't actually
type "extends Object" into the Animal class declaration. It's implicit.) At
this point we're on the top of the stack.
4. Object instance variables are given their explicit values



Animal class constructor is implicitly called when we give a statement like:
Horse h=new Horse();


Animal class constructor in return will call Object class constructor which is its default parent class,means Object class constructor will implicity called by Animal class construtor.
Then why point 3 and 4(above mentioned) says that Object constructor is invoked and Object instance variables are given their explicit values?we are not passing any value to Object class constructor from Horse class or Animal class.Explicit values are those which we pass to a constructor.But here which statement is passing explicit values?
Please clear my doubt.Thanks in Advance.
Priya
-------
SCJP(preparing).
Thanks Noam.Nice Explanation.
Hi!
I have a very small doubt regarding accessing arrays.When long is also one of the java integer type like byte,short and int.Then why using a long index value to access an array causes a compile-time error?

Priya
--------
Prerparing for SCJP.
Iam also not staisfied with venu's case 1 statement.
Hi Sunny,
Final instance variables can be initialized at their declaration.
If they are not initialized in their declarations, they must be intialized in constructors.
---------------------------------------

class Hello{
final int a;
Hello(){
a=5;
}

void show(){
System.out.println("Final instance variable"+a);
}

public static void main(String ar[]){
Hello h= new Hello();
h.show();
}
}
-----------------------------------------

priya...
-------------
studying for scjp.
Hi tushar,
I am also just studying for SCJP.So dont know much.But I will try to explain.

The word constant has a special meaning in Java. It doesn�t just mean �a value that doesn�t change;� a better word for that is immutable. In Java, the word constant �or more precisely, the phrase constant expression�means �a value that is computed at compile-time.�

In Java, a constant value is always computed at compile time

"compile-time constant expression":
A compile time constant expression is an expression whose value can be determined at compile time (during verification), before any part of the program has been executed.

In the case of a compile-time constant the compiler is allowed to �fold� the constant value into any calculations in which it�s used; that is, the calculation can be performed at compile time, eliminating some run-time overhead. In Java, these sorts of constants must be primitives and are expressed using the final keyword. A value must be given at the time of definition of such a constant.

The second statement is creating a Blank Final i.e which are fields that are declared as final but are not given an initialization value. In all cases, the blank final must be initialized before it is used, and the compiler ensures this. However, blank finals provide much more flexibility in the use of the final.


final int a=5;//compile time constant
Here a is initialized final.


final int a;//not compile time constant
Here a is a blank final.
a=5;



Priya Shende
-------------------
Studying for SCJP.
Thanks Mustafa.
-----------------
Studying for SCJP.
hi tushar,
Super class dont know about child class.There is a simple class Animal and having one constructor and we are calling constructor in main()function with proper argument.Then why the code is giving comile time error that can't find symbol.

thanks,
priya.