Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Couple of questions

 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following question on Marcus Greens exam #1
1)
public class Pass
{
static int j=20;
public static void main(String argv[])
{
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(int x)
{
x=x*2;
j=j*2;
}
}
the answer is 10 & 40. Im wondering how. If primitives are passed by value, meaning the value change that occurs within a method is not reflected outside the method, then why does the value of j become 40 and not remain 20. I can see that the value of i is not changed outside the body as expected, but then why is j treated differently?
2)
If a class B is a subclass of A, then
arent
A a=new B();
and
B b1=(sub) new A();
both legal?
If the above is true, then why does the following give a runtime error?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx
{
public static void main(String argv[])
{
Base b=new Base();
Sub s=(Sub) b;
}
}
[ January 12, 2003: Message edited by: Shashank Gokhale ]
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the answer is 10 & 40. Im wondering how. If primitives are passed by value, meaning the value change that occurs within a method is not reflected outside the method, then why does the value of j become 40 and not remain 20. I can see that the value of i is not changed outside the body as expected, but then why is j treated differently?

The difference is that 'j' is a static variable. There is only one 'j' per class so any changes that are made to 'j' are made to the one and only 'j'. 'i' on the other hand is a local variable. The only changes that will affect 'i' are the changes that happen inside the method that contains it.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a class B is a subclass of A, then
arent
A a=new B();
and
B b1=(sub) new A();
both legal?


No. However you can say

A a = new B();
B b1 = (sub)a ;

because 'a' already is a B (which is a sub of A).
On the other hand, a new A() is not a B or a sub of A. It is strictly an A.
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I kinda get it now, more than before.
One more question
I have a class
public class foo
{
String name;
public void say();
{
System.out.println("Hi there, "+name);
}
}
and another
import foo;
public class disp
{
public static void main(String arg[])
{
foo f=new foo();
f.name="Ann";
f.say();
}
}
if both these files are in the same directory, then shouldn't these classes work together
I can successfully compile the foo class with the javac by saying javac foo.java
but for some reason i cant compile the other one. So javac disp.java gives me an error saying
disp.java:1:'.' expected
import foo;
1 error ^
whats going on
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic