0

Im facing a problem whereby I need to use an updating string data from a method in the same class. should i create a global variables? and if so, how could I place the updating string data to the global variables? below is my code whereby comment are added to illustrate more detail.

public class GenerateSummonPDF
{


public void userdata(String p1, String p2, String p3, String p4)
{
    String value1= p1;
    String value2= p2;
    String value3= p3;
    String value4= p4; // all the data here are constantly updating from other class



}
public static void main(String[] args)
{

  Document document = new Document();
  try
  {
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\User\\workspace\\enforement system\\Summon PDF list\\Serial No.pdf"));
     document.open();
     document.add(new Paragraph("")); //i need to print all the data here from the userdata
     document.close();
     writer.close();
  } catch (DocumentException e)
  {
     e.printStackTrace();
  } catch (FileNotFoundException e)
  {
     e.printStackTrace();
  }
 }

Is there any solution for that? Thanks for help in advance.

calvin ern
  • 783
  • 1
  • 6
  • 12

1 Answers1

0

Use get/setmethod for the private String value. Rest you don't have to change anything.

You could read more here: How do getters and setters work?

quoting an answer here:

private String myField; //"private" means access to this is restricted

public String getMyField()
{
     //include validation, logic, logging or whatever you like here
    return this.myField;
}
public void setMyField(String value)
{
     //include more logic
     this.myField = value;
}
Community
  • 1
  • 1
XOR-Manik
  • 493
  • 1
  • 4
  • 19
  • what do you mean actually? because all i got after is a "null", maybe my code has problem – calvin ern Aug 31 '15 at 16:14
  • so if there is more than one value, thats means I needs more of the "public String getMyField()"? is it correct? – calvin ern Aug 31 '15 at 16:26
  • If you are updating/overwriting all of them together, you can do that in 1set of getter and setter method. If they you are overwriting them separately and differently then you ought to have separate set of getter and setter for those fields. – XOR-Manik Aug 31 '15 at 16:28
  • but the "return this.myField;" can only be writen in a single method though. – calvin ern Aug 31 '15 at 16:31
  • For your example: you will have "return this.value1;" "return this.value2;" and so on. All of these return values will be in their respective "getvalue1()","getvalue2()", and so on.. methods. – XOR-Manik Aug 31 '15 at 16:34
  • owh, I've learned the get/set, Thanks to you!!! One more thing and is it legal to make the "private string Field" to "private static String Field"? because when i use in the method it came out error – calvin ern Aug 31 '15 at 16:39
  • No you should not make it Static, that might be evil for your program. The problem you are facing the error is because there are chances that you are referencing the values from a static method is that so? Or share your error so as to have better idea. – XOR-Manik Aug 31 '15 at 16:47
  • "document.add(new Paragraph("'"+value1+"'")); //i need to print all the data here from the userdata document.close();" I need to put the value1 which is the private string i had declared as global variables, and the error is this "Cannot make a static reference to the non-static field value1" – calvin ern Aug 31 '15 at 16:50
  • You can use +getvalue1 instead of +value1. – XOR-Manik Aug 31 '15 at 16:53
  • I still get error for changing to +getvalue1, with this error "getvalue1 cannot resolve to a variables". – calvin ern Aug 31 '15 at 16:56
  • Could you post as a separate question with the code? – XOR-Manik Aug 31 '15 at 17:07
  • sure i'll name it Generating PDF with get/set variables – calvin ern Aug 31 '15 at 17:09
  • It has already an answer I guess which should be working, as you are calling the getter/setter from a non static method. I hope it solves your doubt. – XOR-Manik Aug 31 '15 at 18:13