This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!

kt randhawa

Greenhorn
+ Follow
since May 22, 2006
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 kt randhawa

Is Kathy Sierra for SCJP5.0 availible online? any ideas? plz send the link i anyone has?
When I create 4 threads inside the main method,will the same run() method be called for all of them?
If I declare a new Thread object inside the run method of the class implementing interface Runnable,then which 'run()' is called when the scheduler starts this new Thread object?


class mythread implements Runnable{
public void run()
{
System.out.println("kt");
System.out.println(Thread.currentThread().getName());
Thread y=new Thread();
System.out.println(y.getName());
System.out.println(Thread.currentThread());
y.start();
System.out.println(Thread.currentThread());
}
}
You are correct in saying "for overloading refrence type is considered while for overiding object type is considered., "
but its not true that "overloading can take place in base class and subclass."
Actually overloading occurs in the same class,it could be of the overriden method also but it has to be in same class(subclass or superclass)
in the following example,the method happy() has been overriden in the derived class as well as it has been then overloaded in the derived class.

class some {

public void happy(){
System.out.println("parent");
}

}

public class derived extends some{

public void happy(int a)
{
System.out.println("kirt");
}
public void happy()
{
System.out.println("overriden methd calld");
}
public static void main(String []args)
{
derived d=new derived();
d.happy();
some n=new some();
n.happy();
some m=new derived();
m.happy();
}
}

Just try and execute and observe the output.
Ok,then what else access specifier can we use with/without static for the inner class? Can we have public static inner{} or abstract/final etc for that matter?
If so what is their significance?
Problem: Does access specifiers have the same affect on members of inner classes?as they have on normal class members?

In the code below,I tried to increment the variable kk outside the inner class,it gets incremented but why is this private variable accessible from outer class?

class outer{


static class inner{
private static int y=9;
private int kk=89;
}

public static void main(String []args)
{
System.out.println(new inner().kk);
System.out.println(++(new inner().kk));
}

}
Ok,I want to call the main method from the inner class,so I had put the main method inside the inner class,now Since main has to be static ,so I had to make my inner class as a static class.
Moreover when I make the .java file also with the same name as the inner class,still copiler cannot find the main methods,I also tried making the inner class public.
What could be the solution for this?
If a class is static,what does it mean?
Can only inner classes be static?
If I have a static inner class which contains a non static method ,then how do I access it in the main method of the outer class?

Please clarify my doubts !!!
The following code gives the error below:

java.lang.NoClassDefFoundError: testpack/derived
Exception in thread "main"

The file in which I had compiled this piece of code is also of the name "myfile.java"
Does that mean I cannot have my main method inside an inner class?


class outer {

static int i=8;
int k=9;
static class myfile{
public void happy()
{
System.out.println(i);
}

public static void main(String []args)
{
System.out.println(i);
outer ow=new outer();
System.out.println(ow.k);
}

}
}
According to me,
4 objects are:
1)String s = new string("abc");
creates 2 objects("abc" in pool and "abc" in non pool refered by s)
2)s.concat("def") ; creates 2 objects (one with value "def" in pool and other with value "abcdef" again in pool.
However s still contains value "abc". and "def" and "abcdef" are lost.

Here is another example:
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
What is the output?
For extra credit, how many String objects and how many reference variables were created prior to the println statement? Answer:
The result of this code fragment is �spring winter spring summer�.
There are two reference variables, s1 and s2. There were a total of eight String objects created as follows: �spring�, �summer � (lost), �spring summer�, �fall�(lost), �spring fall� (lost), �spring summer spring� (lost), �winter� (lost), �springwinter� (at this point �spring� is lost). Only two of the eight String objects are not lost in this process.
I think there are 3 objects as below:
1)String s = new string("abc"); creates 2 String objects(one in pool & other in non pool with same value "abc")
2)s.concat("def")
creates 2 objects technically,one with value "def" in pool and other again in pool with value "abcdef" and this one has no reference pointing to it.

However,the reference variable s still holdsthe value "abc".

Here is another example:

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
What is the output?
For extra credit, how many String objects and how many reference variables were created prior to the println statement?
Answer:
The result of this code fragment is �spring winter spring summer�.
There are two reference variables, s1 and s2. There were a total of eight String objects created as follows: �spring�, �summer � (lost), �spring summer�, �fall�(lost), �spring fall� (lost), �spring summer spring� (lost), �winter� (lost), �springwinter� (at this point �spring� is lost). Only two of the eight String objects are not lost in this process.
hi,
what does the 'this' pointer refer to inside a subclass method?
If my class has 3 costructors ,Can I make a call to each of the other two from the first constructor?
I tried this using 'this' but compiler does not allow me to make multiple calls using this? is it correct behavior?

class Dummy {
public Dummy()
{
this(7);
this("lite-1");
}
public Dummy(int e)
{
System.out.println("2");
}
public Dummy(String e)
{
System.out.println("3");

}

}
public class derived
{
public static void main(String[] args)
{
Dummy aa=new Dummy();

}

}

The err message I receive is:

Constructor call must be the first statement in a constructor
Hi,
What reference does a 'this' hold inside a subclass?
In some subclass method,I tried calling some base class method through 'this',it showed the list of methods of its base class after pressing '.'
But I found that 'this' does not get recognised inside main method of the subclass.why?

Originally posted by Srinu Nanduri:
The most common usage of destructors is to free memory - and java will do that for you automatically via "garbage collection"

That said, if you have some other special resource to free or if you're curious as to when the GC runs, implement a finalize() method (protected void finalize()) which will be run when the GC is about to delete the object.

It's important to bear in mind that the finalizer is run when the GC frees your object, not when the object first becomes garbage (i.e. unreferenced) - depending on memory conditions inside the heap on your specific platform and the particulars of the runtime you use, it could be a very long time before the finalizer is called during a GC cycle.



Thanks.