Hi, 1. "this" is a reference to current object and available to all methods, right ? what can be said about its modifiers ? class Test { public final Test this; ... } 2.If we implement an abstract method of an interface in a class, is it correct to say that we are overriding the method ? 3. Is constructor a member method or not ? Is it implicitly static ? 4. I remember reading in the site that "private means private to class but not private to objects. say Test class has a private vbariable int x. If we instantiate two objects obj1 and obj2 of class Test,how can obj1 access obj2.x ? Looking for code snippet if it is possible.
thanks in advance. ------------------ Chary
Chary
Ayman Jaffar
Ranch Hand
Joined: Sep 06, 2000
Posts: 57
posted
0
Hi, About 1- There's no such code as "public final Test this;" and if you compile it, the compiler will complane. And it can't have a public, protected, private, nor friendly modifier, becuase all variables inside a method can't be declared with such modifiers. But "this" is final, becuase it can only be used to change the variable of an Object or to invoke that method of an Object but it can't point to a different Object (like saying this = new Cup () ) 2- yes 3- Constructor is a member method, and I don't see why not. No it's not static becuase there is a constructor for each Object when it's created 4- public class Test { private int x = 1; void privateMethod (int x) { System.out.println("t2.x is " + x + " and t1.x is " + this.x); } public static void main(String[] args) { final Test t1 = new Test(); final Test t2 = new Test(); t1.privateMethod(t2.x); } } I recomend your read the Khalid and Rolf book, for the java certification, (it's at javaranch.com and amazon.com but if you buy it at javaranch you'll help support this site) . I think you lack some basic information but you have to keep going on. Good luck on the certification.
[This message has been edited by Ayman Jaffar (edited November 06, 2000).]
america
yogesh sood
Ranch Hand
Joined: Aug 31, 2000
Posts: 108
posted
0
I will try to answer step by step.and also hope u will also do same 1) u a cannot give any access modifier to this to learn more bout this read my previous reply on this topic http://www.javaranch.com/ubb/Forum24/HTML/005188.html
2)Yes u can say that u are overriding method same thing u do in case of abstract class . 3)Constructor is not member of class it is not inherited by Derived classes neither it is static.constructor can only be public, private, protected ; 4) read Following code class Testc{ private int s; public void set(int temp){s=temp;} public int get(){return s;}
in above code we are creating two object of class Testc and passing them to compare method of third object where we r using if (temp1.s==this.s) mean object are accessing private varible of each other so private restication is only to class not to object if u write these statment in other class u will get error coz varible s of class Testc is not available outside class Testc Hope this will Help [This message has been edited by yogesh sood (edited November 06, 2000).]
If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY