Results 1 to 4 of 4
  1. #1
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default How to access private data types from public classes?

    Suppose I have two classes, Artist and Driver:

    Java Code:
    public class Artist
    {
    	private String bestpicture="Slumdog Millionaire";
    	private String bestforeign="Departures";
    	private String bestcountry="Japan";
    	private String bestdocu="Man on Wire";
    	private String bestanim="WALL-E";
    	private String bestactor1="Sean Penn";
    	private String bestactor1film="Milk";
    	private String bestactress1="Kate Winslet";
    	private String bestactress1film="The Reader";
    	private String bestactor2="Heath Ledger";
    	private String bestactor2film="The Dark Knight";
    	private String bestactress2="Penelope Cruz";
    	private String bestactress2film="Vicky Cristina Barcelona";
    	private String bestdirector="Danny Boyle";
    	private String bestdirectorfilm="Slumdog Millionaire";
    	private String bestscreen1="Dustin Lance Black";
    	private String bestscreen1film="Milk";
    	private String bestscreen2="Simon Beaufoy";
    	private String bestscreen2film="Slumdog Millionaire";
    	public String SetBestPicture(String bestpicture)
    	{
    		this.bestpicture = bestpicture;
    		return bestpicture;
    	}
    	public String SetBestForeign(String bestforeign)
    	{
    		this.bestforeign = bestforeign;
    		return bestforeign;
    	}
    	public String SetBestCountry(String bestcountry)
    	{
    		this.bestcountry = bestcountry;
    		return bestcountry;
    	}
    	public String SetBestDocu(String bestdocu)
    	{
    		this.bestdocu = bestdocu;
    		return bestdocu;
    	}
    	public String SetBestAnim(String bestanim)
    	{
    		this.bestanim = bestanim;
    		return bestanim;
    	}
    	public String SetBestActor1(String bestactor1)
    	{
    		this.bestactor1 = bestactor1;
    		return bestactor1;
    	}
    	public String SetBestActor1Film(String bestactor1film)
    	{
    		this.bestactor1film = bestactor1film;
    		return bestactor1film;
    	}
    	public String SetBestActress1(String bestactress1)
    	{
    		this.bestactress1 = bestactress1;
    		return bestactress1;
    	}
    	public String SetBestActress1Film(String bestactress1film)
    	{
    		this.bestactress1film = bestactress1film;
    		return bestactress1film;
    	}
    	public String SetBestActor2(String bestactor2)
    	{
    		this.bestactor2 = bestactor2;
    		return bestactor2;
    	}
    	public String SetBestActor2Film(String bestactor2film)
    	{
    		this.bestactor2film = bestactor2film;
    		return bestactor2film;
    	}
    	public String SetBestActress2(String bestactress2)
    	{
    		this.bestactress2 = bestactress2;
    		return bestactress2;
    	}
    	public String SetBestActress2Film(String bestactress2film)
    	{
    		this.bestactress2film = bestactress2film;
    		return bestactress2film;
    	}
    	public String SetBestDirector(String bestdirector)
    	{
    		this.bestdirector = bestdirector;
    		return bestdirector;
    	}
    	public String SetBestDirectorFilm(String bestdirectorfilm)
    	{
    		this.bestdirectorfilm = bestdirectorfilm;
    		return bestdirectorfilm;
    	}
    	public String SetBestScreen1(String bestscreen1)
    	{
    		this.bestscreen1 = bestscreen1;
    		return bestscreen1;
    	}
    	public String SetBestScreen1Film(String bestscreen1film)
    	{
    		this.bestscreen1film = bestscreen1film;
    		return bestscreen1film;
    	}
    	public String SetBestScreen2(String screen2)
    	{
    		this.bestscreen2 = bestscreen2;
    		return bestscreen2;
    	}
    	public String SetBestScreen2Film(String bestscreen2film)
    	{
    		this.bestscreen2film = bestscreen2film;
    		return bestscreen2film;
    	}	
    }
    Java Code:
    public class Driver
    {
    	public static Artist alpha = new Artist();
    	public static void main (String args[])
    	{
    		System.out.print("The 81st Academy Awards");
    		System.out.print("\n------------------------");
    		System.out.print("\n\nList of winners:");
    		System.out.print("\n");
    		System.out.print("\nBest Picture: ");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Foreign Language Film: ");
    		
    		System.out.print("\nCountry of origin: ");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Documentary: ");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Animated Feature: ");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Actor in a Leading Role: ");
    		
    		System.out.print("\nFilm:");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Actress in a Leading Role: ");
    		
    		System.out.print("\nFilm:");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Actor in a Supporting Role: ");
    		
    		System.out.print("\nFilm:");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Actress in a Supporting Role: ");
    		
    		System.out.print("\nFilm:");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Director: ");
    		
    		System.out.print("\nFilm:");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Writing - Original Screenplay: ");
    		
    		System.out.print("\nFilm:");
    		
    		System.out.print("\n");
    		System.out.print("\nBest Writing - Adapted Screenplay: ");
    		
    		System.out.print("\nFilm:");
    		
    		System.out.print("\n");
    	}
    }
    And I want the Strings on the Artist class to be accessed by the Driver class. I need help here. What should I do?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    That's a lot of code !-)

    Java Code:
    public class Foo {
        private String str = "Some private String";
    
        public String getStr() {
            return str;
        }
    }
    
    public class Driver {
        public static void main(String args[]) {
            Foo test = new Foo();
            System.out.println("The private string is " + test.getStr());
        }
    }

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    why are you returning the string that you pass into your setter methods? why not just have them be void?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Simply you should have get/set methods to access private members of a class. get/set methods are public, as pbrockway2 shows in his code. It's basically all about Encapsulation in Java.

Similar Threads

  1. Java Native Access (JNA) return types of void *
    By burnumd in forum Advanced Java
    Replies: 5
    Last Post: 01-15-2010, 01:09 AM
  2. Private Classes Clarification
    By justlearning in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 11:51 PM
  3. Replies: 2
    Last Post: 05-05-2008, 11:55 AM
  4. why we use public access modifier?
    By vichet in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 08:04 AM

Posting Permissions

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