0

So I have created a simple class in Java like this:

public class Book {
    private String author;
    private String title;

    public Book (String author, String title) {
        this.author = author;
        this.title = title;
    }
}

public void checkInfo     

Is there a way to parse a string (property) in order to get Book properties like this, instead of doing bookA.title ?

Book bookA = new Book("George Orwell","Animal Farm")

String property = "title";
System.out.print(bookA.property);

Thanks in adance!

DanielleC
  • 127
  • 1
  • 3
  • 10
  • 1
    You could do something like `bookA.get(property);` (after you add a `get(String)` method in `Book`). I would prefer a `getTitle()` and `getAuthor()` (in which case you can access your fields with [JavaBean properties](https://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html)). – Elliott Frisch May 15 '16 at 02:02
  • you need to implement `getter` and `setters` for Class `Book`. It will be more Convenient and better approach which introduced by java. – Vikrant Kashyap May 15 '16 at 02:05
  • I'm guessing that you don't actually intend to have ``static`` in your class definition. (Thinking you'd want multiple instances of ``Book``) – chsbellboy May 15 '16 at 02:08
  • @chsbellboy right, will edit that – DanielleC May 15 '16 at 02:09
  • Thanks so much for the comments, the reason why i didnt implement getter methods is because, it the real case im working with, there will be a lot more properties, intead of just author and title. So maybe getter is the only way to accomplish this? – DanielleC May 15 '16 at 02:11
  • Doesn't matter, add Getters for all of them. – UDKOX May 15 '16 at 02:16

7 Answers7

2

If you really want to access many properties as String, I suggest you using a Map<String, String> like this :

public class Book
{
    private Map<String, String> properties = new HashMap();

    public void setProperty(String name, String value)
    {
        properties.set(name,string);
    }

    public String getProperty(String name)
    {
        return properties.get(name);
    }
}

Now you can use like this :

Book book = new Book();

book.setProperty("title","Animal Farm");
book.setProperty("author","George Orwell");

System.out.println("Book: " + book.getProperty("title") + " by " + book.getProperty("author"))
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
1

You've created your Book as an object.
So, treat it like an object and add getters and setters.

In this case, that would be a method, getTitle() and a separate method getAuthor().
For more information on getters and setters, see the responses to this previous StackOverflow post

Community
  • 1
  • 1
chsbellboy
  • 490
  • 2
  • 15
1

You can use reflection:

 Field f = bookA.getClass().getDeclaredField("title");
 f.setAccessible(true);
 String title = (String) f.get(bookA);
 System.out.println(title);
IT_Nike
  • 91
  • 6
1

First of all, your code won't work because title is private. Second, I have no idea why you set Book class as static. Last, this (Java) is object oriented programming, so treat it like an object.

When you create a class you also add Getters & Setters to access the information inside. The code would look like this:

Class:

public class Book {
    private String author;
    private String title;

    public Book (String author, String title) {
        this.author = author;
        this.title = title;
    }
}

public String getTitle(){
    return this.title;
}

public String getAuthor(){
    return this.author;
}

Accessing the data:

Book bookA = new Book("George Orwell","Animal Farm")

System.out.print("Book: " + bookA.getTitle() + " by " + bookA.getAuthor());

This would return :

Book: Animal Farm by George Orwell
UDKOX
  • 738
  • 3
  • 15
0

If you see these few lines from your code:

private String author;  // both are private variables
private String title;  

Here author and title both are private String . So you can't access these properties outside of the class.

So, you'll need to add public getters and setters that can be used to access the properties.

chsbellboy
  • 490
  • 2
  • 15
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
0

you should change you Object class.. add getter and setter method.. here is example :

public class Book{ 
  String myauthor;
  String mytitle;
public Book (String author, String title){
  myauthor=author;
  mytitle=title;
}
public void setAuthor(String Autor){
myauthor=author;
}
public String getAuthor(){
return myauthor;
}
}

and create setter and getter for 'title' too.. if you want to get the title / author, just simply call

Book.getAuthor();
Diastowo
  • 42
  • 1
  • 10
0

If you don't want to have getter/setter method for your class ;you can define access modifier as protected with static keyword.For example is: under com.test package-There are two class.One is Book class and other is BookInSamePackage class.In Book class;if you define attribute title as protected static String title then in BookInSamePackage class ;You can access like that :'Book.title'.If you want to use this title attribute in class of another package;then this class need to extend Book class and can access like this way:Book.title in child class of another package.

sawyinwaimon
  • 739
  • 1
  • 6
  • 14