aspose file tools*
The moose likes Beginning Java and the fly likes Couple of questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Couple of questions" Watch "Couple of questions" New topic
Author

Couple of questions

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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 ]

May the force of the Java be in all of us !!!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10

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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
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

Joined: Jan 07, 2003
Posts: 92
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Couple of questions