Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-03-2008, 07:37 PM   #1 (permalink)
waynew
Recruit
 
Join Date: Aug 2008
Posts: 4
waynew is on a distinguished road
Unhappy Variable Reference

Hello, I'm new to OOP and Java and I have a problem I have been unable to solve.
I have searched many books and online and can't seem to find a solution that works.
Hoping someone here can enlighten me as to what I am doing wrong.

I have some user preferences in a desktop application that I want to be able to
access from several classes in my package.
Specifically, I need to set some variables in a class then access the values from other classes.
Here is a representation of what I am trying to do:
Code:
public class A { // the setup class, initialize and set the variable value
       
        private String aString = "";

        public A {
        }

        public void setAString(String aString) {
            this.aString = aString;
        }

        public String getAString() {
            return aString;
        }

        public void initString() { // actually reads the value from a file in my code
          aString = "abc";
        }
}

public class B { // try to initialize the variable and get the value from class A

        public initVar() {
                A a = new A();
                a.initString(); // makes class A set the variable value  (read the file in my code)
        }

        public doSomething() {
                //get the variable value from class A
                String newString = a.getString(); // returns null from class A
        }
}
Why does a.getString in doSomething return null instead of "abc"?
What do I need to change?
Thanks if you can help!

Last edited by Belisarius; 08-17-2008 at 06:55 AM. Reason: Inserted code tags
waynew is offline   Reply With Quote
Old 08-17-2008, 06:59 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
I'm surprised that even compiles - in class B you don't define "a" either in the class of the "doSomething" method - you only define it in "initVar";

Try putting
Code:
A a = new A();
in the class scope - I'm willing to bet you won't get null anymore.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-17-2008, 07:04 AM   #3 (permalink)
waynew
Recruit
 
Join Date: Aug 2008
Posts: 4
waynew is on a distinguished road
Thanks

Thanks for your help. Yep, it was probably a bad example.
I have since solved the overall problem by using a SingletonObject and it is working fine.
waynew is offline   Reply With Quote
Old 08-17-2008, 07:56 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Glad you were able to find a solution. Yes, a singleton design pattern is generally the best way to go for configuration objects. Usually I'm quicker at responding to help requests - if you encounter any more difficulty feel free to stop by.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-17-2008, 08:52 AM   #5 (permalink)
waynew
Recruit
 
Join Date: Aug 2008
Posts: 4
waynew is on a distinguished road
Another question

Well then, I have another, unrelated Java question, being a real newbie.
I need to add the ability to my desktop application to capture an RTP audio stream and also to generate an RTP audio stream to transmit.

Can I do this without having to use JMF?
waynew is offline   Reply With Quote
Old 08-17-2008, 07:41 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Well, that's really not a "newbie" question. No harm in asking - it's just that the solution might be a bit more advanced than you're willing to tackle. I hadn't heard of either of those acronyms before, but poking around a bit it looks like your plan to try and cut out JMF is a good one. Wikipedia mentions several alternatives, but you'd need to investigate them and see if any of them meet your needs.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-18-2008, 02:29 AM   #7 (permalink)
waynew
Recruit
 
Join Date: Aug 2008
Posts: 4
waynew is on a distinguished road
Smile Newbie Question

Well since this is my first Java program, anything I don't know seems like a newbie question.
I have been researching the Sun docs and the forums, including posting a question in the Sun Media forum, but no luck so far.
It looks simpler to use the Sound api as JMF is just more overhead than I need and I am concerned about cross-platform limitations in JMF.
Thanks for your suggestion. I'll keep looking and at last resort, just code it using Sound and see if it works.
waynew is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search $stringreplaced variable for I's and e's Love.Oasis PHP 1 12-30-2007 08:47 PM
DevC++ Help clavezza Platform/API C++ 2 04-13-2006 12:19 PM
reset Variable in HTML awieds MS Technologies ( ASP, VB, C#, .NET ) 2 04-12-2005 12:32 AM
Is a Static Reference to an Object Garbage Collected? dancer Java 1 03-01-2005 11:50 AM


All times are GMT -8. The time now is 12:04 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting