Results 1 to 5 of 5

Thread: Get {} Set {}?

  1. #1

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Get {} Set {}?

    In C# you can do this

    Code:
    private int iCoins;
    public int Coins
    {
    	Get
    	{
    		return this.iCoins;
    	}
    	Set
    	{
    		this.iCoins = value;
    	}
    }
    is there a way to do something like this in java

  2. #2
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Get {} Set {}?

    I dont have a java compiler on the computer i am using, so i dont know if this syntax is allowed. Try this.
    Code:
    private int iCoins;
    
    public int getCoins(int value)
     {
         //Set 'iCoins'
         this.iCoins = value;
         //Return the value of 'iCoins'
         return iCoins;
     }
    I dont think you need to use this when returning iCoins.
    Last edited by x-ice; Nov 1st, 2005 at 01:38 PM.

  3. #3
    New Member Santa Clause's Avatar
    Join Date
    Dec 2003
    Location
    North Pole. Lapland when I visit my Wife.
    Posts
    10

    Re: Get {} Set {}?

    In java you have to write the accessors yourself:
    Code:
    class Person 
    {
        private String firstName;
        private String surName;
    
        void setFirstName(String s)
        {
            firstName = s;
        }
    
        String getFirstName()
        {
             return firstName;
        }
    
        String getSurName()
        {
            return SurName;
        }
    }
    SurName would be read-only as you have no set method for it. To get and set the properties:
    Code:
    p = new Person();
    
    p.setFirstName('Santa');
    I am the real Santa Clause

  4. #4

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Get {} Set {}?

    damn i was hoping that i would be able to do

    Code:
    class yyy
    {
    	private String sMyString;
    	public String MyString
    	{
    		get
    		{
    			return this.sMyString;
    		}
    		set
    		{
    			this.sMyString = value;
    			System.out.writeln("MyString has been changed to: " + value);
    		}
    	}
    }
    class xxx
    {
    	public xxx()
    	{
    		yyy y = new yyy();
    		y.MyString = "Hello World!";
    	}
    }
    or something alone those line.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Get {} Set {}?

    Nope. Properties don't exist in Java proper. (No loss IMHO.) Bean scripting environments, however, will use the get/set pattern to create properties.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width