LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2005, 09:25 PM   #1
zeppelin147
LQ Newbie
 
Registered: Nov 2005
Posts: 4

Rep: Reputation: 0
java variable scope - use of "this" keyword


I cannot get the following piece of code to behave. The test file I am using passes the string "hello" to SimpleEmail, nut the output that I get is "unknown", which means that when I use "this.(variablename)", it is actually accessing the global variable, and not the local one. Any help would be greatly appreciated.

import java.io.*;

public class SimpleEmail
{
public static String sender = "unknown", recipient = "unknown", subject = "unknown", content = "unknown";


public SimpleEmail(String sender, String recipient, String subject, String content)
{

sender = this.sender;
recipient = this.recipient;
subject = this.subject;
content = this.content;

System.out.println("sender =" + sender + " this sender = " + this.sender);
System.out.println("subject = "+ subject+" this subject = "+ this.subject);
}

}
 
Old 11-21-2005, 11:04 PM   #2
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Rep: Reputation: 30
remove 'static' keyword

and assign like this:

this.sender = sender;
etc.

Now if you want to access the local variable use 'sender'
if you want to access instance variable use 'this.sender'.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
exportfs: /etc/exports:1 unknown keyword "show" ukrainet Linux - Newbie 2 12-15-2004 08:18 AM
Java error "Exception in thread "main" java.lang.StackOverflowError" nro Programming 1 09-04-2004 04:47 AM
what is the scope of "root"? SciYro Linux - General 12 06-06-2004 05:37 AM
I cannot use "java chat". Browser says plugin required "x-java-vm". jdruin Linux - Software 4 04-18-2004 06:44 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 02:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration