• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Couple of questions

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Everybody's invited. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic