Win a copy of Head First Android this week in the Android forum!

Himanshu Saxena

Greenhorn
+ Follow
since Jul 18, 2007
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 Himanshu Saxena

Hi
First of all I want to tell that the default constuctor is called.
Second the Variables depend on the refernce type while the methods depend on the object type.

Try this:

class Phone {
static String device = "Phone.device";
void showDevice() {
System.out.println("Phone.showDevice," + device + " ");
}
Phone() {
System.out.println("Device:"+device);
showDevice();
}
}

class Mobile extends Phone {
String device = "Mobile.device";
void showDevice() {
System.out.println("Mobile.showDevice," + device + " ");
}
Mobile() {
System.out.println("Device:"+device);
showDevice();
}
public static void main(String[] args) {
Mobile n = new Mobile();
n.showDevice();
}
}

Output:
Device hone.device
Mobile.showDevice,null
Device:Mobile.device
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device
Hi you are trying to use a non static field inside a static method.
static method can access only the static fields.
Learn about static ?



package pack2;
import java.awt.*;
import pack1.*;
public class pack2subclassotherfile extends pack1class
{
public static void main(String[] sCommand)
{
System.out.println("protected method"+prot);// not a static field
protMethod();
}

}

use this:

package pack2;
import java.awt.*;
import pack1.*;

public class pack2subclassotherfile extends pack1class{
String s=prot; //acess protected variable
public static void main(String[] sCommand)
{
pack2subclassotherfile a=new pack2subclassotherfile();
System.out.println("protected method"+a.prot);
a.protMethod();
}

}
Hi your answers are as follow

Q:Assertions are disabled at run time by default right? So why compiler is giving 'Unreachable' error at line 20.
A: Unreachable code is not dependent on assertion, it is compiler task.
so you get it in any code example:
public void m(){
while(true){
System.out.println("Inside");
}
System.out.println("Outside");
}

Q;Why it does not give error at line 14, as assertion at line 13 is false and compiler knows that it is going to throw AssertionError at line 13.
A: A method throw AssertionError at runtime not compile time.
Hi,

But still the question remains unanswered why is it happening ?
If you are saying that because we declare it final so it automatically
narrow the return type.And implicity it cast from char(16 bit ) to byte (8 bit). Then why it does not happen all times.

Eg it is not the case with other data types.
public int m2(){
final long a=10;
return a; //Error,Implicitly not cast long to int WHY ?
}
But for int & it's subtype byte,char it does
public byte m1(){
final int b=10;
return b; //No Error,Implicitly cast int to byte WHY implicit conversion
}

Tell Me logically.
Hi,

I want to know if we declare char as final then how the
implicit cast from char to byte happens.

static byte m(){
final char c='a'; //try without final, it's compilation error
return c;
}