aspose file tools
The moose likes Beginning Java and the fly likes Which is a better approach ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Which is a better approach ??" Watch "Which is a better approach ??" New topic
Author

Which is a better approach ??

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hello all,
Here is a beginners question.
Which is a better approach among the following
1. class A
{
String sTemp;

public A()
{
this.sTemp = "";
}
}
2.
class A
{
String sTemp = "";
}
I Feel approach 2 is better?
What do u feel?
I would like to know if there are any overheads in the first approach
Thanks,
Sajee Joseph.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
In my experience, which is far outweighed by most in this forum, the "this" command will call from the constructor in that class from which you are working. Your code does have a constructor so I would gather that nothing different would happen as you may already know. As far as overhead I would also gather that since the "this" command is not needed then overhead would be next to nill.
You may want to look at this thread:

HTH
Stevehttp://www.coderanch.com/t/395349/java/java/Difference-xxx-xxx
[ January 20, 2004: Message edited by: Steve Wysocki ]
[ January 21, 2004: Message edited by: Steve Wysocki ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

Well, from a performance standpoint, there's absolutely no difference; both compile to identical code. You can see this by compiling each version and then looking at the bytecodes using "javap -c A" .
From a style standpoint, as the second version is both shorter and clearer, I agree that's it's better.


[Jess in Action][AskingGoodQuestions]
Ram Mohan
Ranch Hand

Joined: Jan 19, 2004
Posts: 44
i have to disagree with u all....i'm getting confused here, but wouldn't the variable sTemp be available inside the object only inside the first way??? ok, obviously its only a temporary variable (from your name definition), but then again how would i know "how temporary" the variable is...?


If quitters never win, and winners never cheat, who's the idiot that said: Quit while you're ahead?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Take another look at the code examples. sTemp is not a temporary variable, it's an instance variable in both examples.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ram Mohan
Ranch Hand

Joined: Jan 19, 2004
Posts: 44
so does that mean anything declared in a constructor becomes an instance variable?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
No. sTemp is not declared inside a constructor; it's declared by the line "String sTemp;" which is outside any constructor or method, but inside the class.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is a better approach ??
 
Similar Threads
Date format conversion
getProperty()
String Manipulation
Calculatin number of words repeating in one sentence
String Tokenizer