6 Replies - 222 Views - Last Post: 10 March 2013 - 08:49 AM Rate Topic: -----

#1 hycfxduisknbfhjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-March 13

call private method within public error finding symbol

Posted 10 March 2013 - 07:05 AM

Trying to create a disk with 3 rotating rings of characters where the outer ring is fixed. The initial constructor for the data type must call a private constructor in order prevent the user from modifying the outer disk. Tried implementing code this way:

  

public ThreeRingDisk(){ //only constructor available to a client
    middle = new ArrayList<Character>();
    inner = new ArrayList<Character>();
    outer = ThreeRingDisk(outr);
  }
  
   private ThreeRingDisk(String outr){ //can only be called by the creator
    outer = new ArrayList<Character>();
    setOuter(outr);
  }



Also, tried rearranging so of methods. Not sure how else to call the private method within the public so that it does not display an error (Error: cannot find symbol).

Is This A Good Question/Topic? 0
  • +

Replies To: call private method within public error finding symbol

#2 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: call private method within public error finding symbol

Posted 10 March 2013 - 07:30 AM

You seem to be a little confused about variable scope. You refer to the local String 'outr' but this variable is invisible to all code outside of the constructor. I would advise that it just be initialised as an instance variable, but that would be ignorant to an underlying problem.

Take this example of variable scope:
Class Test {
  String instanceVariable = "not set"; //instance variable of class Test
  
  Test(){} //empty no-arg constructor
  Test(String localVariable) { //1-arg Constructor with local variable
    instanceVariable = localVariable; //set the instance variable to the value
                                      //of the local variable recieved at construction
  }
  
  public static void main(String[] args) {
    Test testObjectInstance1 = new Test();
    System.out.println(testObjectInstance1.instanceVariable);
    //This will print "not set" because an argument was not given to be
    //the local variable's value, so the 1-arg constructor is not called

    Test testObjectInstance2 = new Test("warblegarble");
    System.out.println(testObjectInstance2.instanceVariable);
    //This will print "warblegarble" because that primitive string value
    //was passed to the 1-arg constructor, causing it to be called and
    //setting localVariable to "warblegarble". The process continues: instanceVariable
    //is set to the same value as localVariable, and then localVariable is discarded.



I hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: call private method within public error finding symbol

Posted 10 March 2013 - 07:31 AM

Which symbol can't it find? Please post the exact error message you're getting - not just the unimportant part. Also where is outr coming from on line 4?

That said, your problem (or at least one of your problems) is that ThreeRingDisk(String outr), as defined on line 7, is a constructor - not method. You can't call it using method syntax like you're trying to do. You can only call it when creating ThreeRingDisk objects using new or from the first line of another constructor by using the this(...); syntax.
Was This Post Helpful? 1
  • +
  • -

#4 hycfxduisknbfhjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-March 13

Re: call private method within public error finding symbol

Posted 10 March 2013 - 08:16 AM

Here is more the code which may be helpful:

public class ThreeRingDisk{
  private ArrayList<Character> outer; //outer rotor
  private ArrayList<Character> middle; //middle rotor
  private ArrayList<Character> inner; // inner rotor

  String outr = "#HBCA";
  
  public ThreeRingDisk(){ //only constructor available to a client
    this.outer = ;
    middle = new ArrayList<Character>();
    inner = new ArrayList<Character>();
  }
  
   public ThreeRingDisk(String outr){ //can only be called by the creator
    outer = new ArrayList<Character>();
    setOuter(outr);
  }

private void setOuter(String outr){   //only the creator can set the outer rotor
    char[] c = outr.toCharArray();     //converts string to char array
    
    for(int i=0; i<c.length; i++){    //add each character to array list
      outer.add(c[i]);
    }




I tried implementing this to call the private constructor within the public constructor; however, I am still confused how to go about it because this.outer must be an array list of characters while outr (called by the private constructor) is a string. the setOuter method converts the string to an array list of characters, but does not return anything. Is using this.outer the proper way to go about using this?
Was This Post Helpful? 0
  • +
  • -

#5 hycfxduisknbfhjr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-March 13

Re: call private method within public error finding symbol

Posted 10 March 2013 - 08:23 AM

I have the ThreeRingDisk(String outr) constructor set to public in that code for troubleshooting purposes, but it will eventually need to be private.
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: call private method within public error finding symbol

Posted 10 March 2013 - 08:35 AM

I don't think access privacy is causing your problem.

You are incorrectly using 'this'. It should be referencing your constructor, not variable.

this("#HBCA");



This code will call your constructor with a string as a parameter. What I am not sure of is how access modifiers will affect it.
Was This Post Helpful? 2
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: call private method within public error finding symbol

Posted 10 March 2013 - 08:49 AM

View PostFlukeshot, on 10 March 2013 - 04:35 PM, said:

What I am not sure of is how access modifiers will affect it.


Not at all. Access modifiers only affect what you can do from outside the class.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1