Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 46
Like Tree1Likes

Thread: How to access a variable in one class from another?

  1. #21
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Re: How to access a variable in one class from another?

    No, 0 and null is not the same thing. Null is when you pass along a non-object, and 0 is a value. What you need to do is to create a new bank before sending it to the constructors. Not creating one bank every time you need to make a new object, mind you, but only one bank period.

  2. #22
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: How to access a variable in one class from another?

    Hi, I am trying to follow this but you have used every form of formatting available for your code. Could you please just use the Java formatting (#) in the advanced editor. Just highlight your code and then click the # sign.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #23
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    ok so I can create a new instance in my bank class and then set that to null

  4. #24
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    Ok I did as you said and it now looks like this, I know I'm still missing something though:

    Java Code:
    public class bankStuff {
        
        private int bank;  //I don't think I'm using this
        
        
        public bankStuff(){
            bankStuff banking=new bankStuff();
            banking=null;
            
    
    //here is my town class and is the same
    
     static bankStuff bankToUse;
    
        public TownRevisit(bankStuff bankToUse) {
            initComponents();
            this.setLocationRelativeTo(null);
            this.bankToUse=bankToUse;
            
            //bankStuff thebank=new bankStuff();
            //otherbank=thebank.getBank();
            
            
        }
        }
    So I tried running this and I still get null in the port class JFrame. I know I am missing something important. My town class JFrame works fine though.

  5. #25
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Re: How to access a variable in one class from another?

    Is this an assignment for a class or something? Or part of a book?

  6. #26
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    Actually I am just trying to learn the basics.

  7. #27
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    I'm not in school right now, I just want to learn how to program.

  8. #28
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Re: How to access a variable in one class from another?

    Thing is, you're lacking a lot of the basics. What null is, how constructors work, etc. I'd suggest looking through The Java™ Tutorials
    DarrylBurke likes this.

  9. #29
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to access a variable in one class from another?

    imho, if only one Bank is to be used, that Bank object should be a singleton object; there would be no need to pass that (single) Bank object around; just ask the Bank class for that single object . Bcause that Bank object will be used by your application, it can be an eager singleton (i.e. pre-created by the Bank class).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #30
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    Singleton object, can't say I have really used that before. To be honest people on the forums make a lot more sense than the java tutorials. I just want to know what I can do to get my code working right. An example would clear up everything. I feel like I almost have it.

  11. #31
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    Would an enum work in this case?

  12. #32
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Re: How to access a variable in one class from another?

    I've already given you an example on how it could work. And how would an enum work in this case? It's like putting an orange in a carwash...

  13. #33
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to access a variable in one class from another?

    Quote Originally Posted by Snake006 View Post
    Would an enum work in this case?
    An enum can do the job here, but it can do so because an enum instance is inherently a singleton. A Bank singleton can be as simple as this:

    Java Code:
    public class Bank {
       private static Bank bank= new Bank(); // the single bank
       public static Bank getBank() { return bank; }
       private Bank() { ... } // no access to the ctor from outside the class
       ...
    }
    Pay close attention to what is public and what not.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #34
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Re: How to access a variable in one class from another?

    Sorry, I've just been in a really crabby mood all day today... I think I'll just stop posting for now.

  15. #35
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    Ok that sounds good. How should I implement the getbank method? My code currently looks like this:

    static bankStuff bankToUse;
    public Town(bankStuff bankToUse) {
    initComponents();
    this.setLocationRelativeTo(null);
    this.bankToUse=bankToUse;

    //bankStuff thebank=new bankStuff();
    //otherbank=thebank.getBank(); It should kind of look like this I think


    }

  16. #36
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to access a variable in one class from another?

    Don't pass a Bank around as a parameter; get the bank as Bank.getBank(); it returns the unique bank over and over again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #37
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    ok I did that. So do I just need to make a local variable for the town class and port class now? Whenever I tried to do that have a local variable called bank=bankStuff.getBank() it could not compare an int to bankStuff? I thought bankStuff was of type int?

  18. #38
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    I should probably clarify too, my town class and port class need to update my bank everytime I buy something. How high my bank value is, depends on how many times I clicked on the work button. Buying an item from the store looked like this when I used local variables:
    if(){ //something is bought
    townbank=townbank-10;
    bankLabel.setText("Gold: " + townbank);
    }
    townbank is an int but the bankstuff is a value. How should I rewrite that?

  19. #39
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: How to access a variable in one class from another?

    Kindly go through the links I posted at #2.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  20. #40
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: How to access a variable in one class from another?

    I can look at that but it will take awhile to find my answer

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. How to Access and Set variable in parent class
    By java_newbie88 in forum New To Java
    Replies: 6
    Last Post: 01-20-2011, 02:48 AM
  2. how to access JSP variable in javascript
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-31-2010, 04:41 PM
  3. variable access from another file
    By riadmazloum in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:13 PM
  4. Access a variable
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •