File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Basic Java doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Java doubts" Watch "Basic Java doubts" New topic
Author

Basic Java doubts

naveen chinthakunta
Greenhorn

Joined: Sep 26, 2005
Posts: 16
Hi all,

I have a couple of doubts regarding Java concepts.

1. In Java methods, primitive types are passed by value and objects are passed by reference. So if I dont want my object value to change in the function, how do I achieve that. Basically how do I pass objects by value?

2. Java completely does away with multiple inheritance. If my requirement demands me to inherit from two classes how do I achieve this? Whats the work around?

Plz help me getting these concepts clear. Thanks in advance.

navEEn
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by naveen chinthakunta:
... In Java methods, primitive types are passed by value and objects are passed by reference. So if I dont want my object value to change in the function, how do I achieve that. Basically how do I pass objects by value? ...

Java always passes by value. In the case of objects, it's a copy of the reference (a value) that is passed. Note however, that the original reference and the copy of that reference both point to the same object. So even though the reference copy is local to a method, the object itself can still be modified via that reference (assuming the object is mutable).

So, if I'm understanding your question, you may want to clone your object in order to get a "local" copy that will not affect the original object. For details on this, see Appendix A: Passing & Returning Objects from Bruce Eckel's Thinking in Java. (Interestingly, this appendix frames Java's "pass by value" vs. "pass by reference" confusion as a debate in which both sides have merit.)
[ November 02, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by naveen chinthakunta:
on, how do I achieve that. Basically how do I pass objects by value?

2. Java completely does away with multiple inheritance. If my requirement demands me to inherit from two classes how do I achieve this? Whats the work around?


Java doesn't do away with multiple inheritance of type, only multiple inheritance of implementation. You can extend only one class, but you can implement as many interfaces as you'd like. Therefore, Java class hierarchies should be designed around interfaces, not around classes.

If you'd like to inherit implementation from multiple classes for convenience sake, though, you can use delegation. Extend one class, then for the others, hold a member of that other class, and forward methods to it. This works great as long as you don't try to treat classes as types; only treat interfaces as types.


[Jess in Action][AskingGoodQuestions]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

1. In Java methods, primitive types are passed by value and objects are passed by reference. So if I dont want my object value to change in the function, how do I achieve that. Basically how do I pass objects by value?


I could go into the whole 'pass by copy of a reference' argument, but I always found it to be too technical, whereas the concept is, yes you are conceptually passing object by reference. If you don't want an object to change you have to deep clone it. If you alternatively want a primitive to change you have to put it in an object like an array.

There are alternate strategies for duplication such as using good encapsulation. For example, if all class members must only be accessed via getters and there are no setters (or limited access to them) you could prevent someone from modifying the object. You have to watch out for complex data types, though, such as an array where the person is free to modify elements of it the array using a getArray() method. In that example, you would need a getter for the primitive elements such as getArrayElement(int).

2. Java completely does away with multiple inheritance. If my requirement demands me to inherit from two classes how do I achieve this? Whats the work around?


Interfaces and helper classes. Multiple Inheritence (via implementation) can be deadly to large-scale applications. Classes can use multiple interfaces and in if organized well, this strategy often leads to cleaner code management.
[ November 02, 2005: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic Java doubts
 
Similar Threads
Passed with 81%
Passed!!
Passed OCPJWCD with 95% today..
Passed SCWCD
passed