Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Changing from Public to private

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Changing from Public to private

    So I have some code here, and I want to change the "reactionFactor" variable in "ReactionAgent" to private or protected and still be able to access it in the next class "Augmentium"
    when i cahnge it to private it tells me that i cant access it because It is private.. I dont want the rectionFactor variable to be changed in the "ReactionAgent" class, only in the instance in the "Augmentium" class.


    I panic so much when I cant get these things done. Im new to java and im wondering if i should just switch to another major or dropout ....I appreciate all help


    Here are the two classes:



    package ReactionAgent;
     
    public class ReactionAgent 
    {
    	public int reactionFactor;  //I want this to be private 
     
    	public ReactionAgent()
    	{
    	reactionFactor = 1;
    	}
     
    	public void setFactor (int Factor)
    	{
    	reactionFactor = Factor;
     
    	}
     
    	public int getFactor()
    	{
    	return reactionFactor;
    	}
     
     
     
     
    }

     
     
     
    import ReactionAgent.ReactionAgent;
    import java.util.Random; // import this library
    public class Augmentium
    {
    	private int val;
    	private int max; 	
    	private int nextInt;
    	public void Augmentium()
    	{
    	val = 1;
    	max = 4;
    	nextInt = 1;
    	}	
     
     
     
     
     
    	ReactionAgent Augmentium = new ReactionAgent();
    	/*public int oreReaction(int Ore)
    	{
    	Ore = reactionFactor + Ore;
    	return Ore;
    	}*/
     
     
     
     
    	Random randNum = new Random();
     
     
    	public int randNum()
    	{
    	max = 3;
     
     
    	Augmentium.reactionFactor = randNum.nextInt(max) + 2; //gets a value between 2 and max(inclusive)
    	return Augmentium.reactionFactor;
    	}	
     
     
    	 public static void main(String args[])
        {
            Augmentium test = new Augmentium();
    		System.out.println(test.randNum());
     
     
       }
    Last edited by Peacetoyou; September 24th, 2019 at 05:59 PM. Reason: formatting

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Changing from Public to private

    Can you use getter and setter methods to allow access to the value in the variable?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Changing from Public to private

    thankyou and how do you mean

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,475
    Thanks
    53
    Thanked 2,405 Times in 2,358 Posts

    Default Re: Changing from Public to private

    Use a getter method to access the value of variable.
    Use a setter method to set the value of the variable.

    See the ReactionAgent class.
    It has a getFactor method
    and a setFactor method.

    An example:
       refToClass.setValue(newValue);    //  set the Value to newValue
       value = refToClass.getValue();     //  get the Value
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Peacetoyou (September 24th, 2019)

Similar Threads

  1. Need understanding for public and private.
    By pratik5317 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2013, 06:28 AM
  2. Chatting application in java (PUblic And private)
    By nakul dev in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2012, 12:27 PM
  3. Convert string to private and public key
    By Ganezan in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 5th, 2010, 08:37 AM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM

Tags for this Thread