• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

variables

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Que.
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
System.out.println(v.i);
}//End of amethod
public int another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ ","+i);
}//End of another
}
I run the program and get result (10,20),Why don't get result(10,10).I think v=vh has sent vh object's fields to v object.why when print vh.i ,it dose not print v object's i.Who know reason.
Thank you.
2)Question
Interface don't use new method
example: Runnable r=new Runnable();It;s wrong I know reason.
But following question.
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef ob = new ObRef();
Base b = new Base();
Object o1 = new Object();
IFace o2 = new CFace();//Why it can use new method
}
}
1)o1=o2;
2)b=ob;
3)ob=b;
4)o1=b;
Answer is 1,2,4.
Why 1 is correct.I compile the program.The answer is right.But I don't know reason.Who know ?
Thank you help.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)vh is the reference created inside the another().that is valid only for that method ie another method.v.i is public and that value is changed to v.i 20.
ie the reference ie v when u change the the value in the reference it changes the original value.ie v.i=20
but vh is instantiated inside the method.so not available in the main method.so v.i will have the same value of 20;since v=vh is valid only for the another method()
2)
u should know about the hierarchy.
u can always store the interface reference in an object;
it comes like this
object
|
|
interface
|
|
class a
|
|
class b extends a
in this u can store the reference from downwards
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would seriously suggest you to read some good java books like Thinking in Java ( get it for free from www.eckelobjects.com ) and the Java Tutorial on Sun site. The questions that you have asked are very fundamental. It won't do you much good even if somebody tells you the answers.
-Paul.
------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!
 
Trailboss
Posts: 23247
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PROPER NAMES ARE NOW REQUIRED!
See http://www.javaranch.com/ubb/Forum10/HTML/000180.html for details.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic