Results 1 to 4 of 4
- 03-07-2009, 01:20 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
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; } }And I want the Strings on the Artist class to be accessed by the Driver class. I need help here. What should I do?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"); } }
- 03-07-2009, 01:25 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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()); } }
- 03-07-2009, 01:47 AM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
why are you returning the string that you pass into your setter methods? why not just have them be void?
- 03-07-2009, 04:19 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Java Native Access (JNA) return types of void *
By burnumd in forum Advanced JavaReplies: 5Last Post: 01-15-2010, 12:09 AM -
Private Classes Clarification
By justlearning in forum New To JavaReplies: 1Last Post: 05-06-2008, 10:51 PM -
[SOLVED] instantiating a class from other classes of different types...
By olbion in forum New To JavaReplies: 2Last Post: 05-05-2008, 10:55 AM -
why we use public access modifier?
By vichet in forum New To JavaReplies: 1Last Post: 04-04-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks