i think you are very confused with modifiers and methods
. it seems that both of your methods are in the same class
.and what you want to do is pass a value to another method
and it has nothing to do with public or private
(modifiers) what you should do is
make a parameter
inside your method, in your case
private void compareAnswers(String input) {
//compare input to the ArrayList
}
I used
compareAnswers(String input) {}
in conjunction with
compareAnswers(input);
Let's say you have all this code in class abc
So it should look something like this:
class abc {
private String Input;
//Some other variables declared.
//some constructors
private void questionSubmitActionPerformed(java.awt.event.ActionEvent evt) {
//gets input and clears answer area
input = questionAnswer.getText();
//calls the comparison, returns Yes or No
compareAnswers();
}
private void compareAnswers() {
//compare input to the ArrayList
}
//Some other methods.
}
Now, still if you insist that the variable be passed to the method compareAnswers
then change the signature of the method to :
private void compareAnswers(String input)
and also call the method inside questionSubmitActionPerformed
as :
compareAnswers(input);
A Java private keyword is an access modifier. It can be assigned to variables, methods, and inner classes. It is the most restricted type of access modifier.,According to the previous point, if we assign a private modifier to any method or variable, that method or variable can be overridden to sub-class using all type of access modifiers. However, still, we can't invoke private method outside the class.,The private access modifier is accessible only within the same class.,Let's see an example to determine whether we can assign the private modifier to the outer class.
Exception in thread "main"
java.lang.Error: Unresolved compilation problem:
The field A.msg is not visible
Exception in thread "main"
java.lang.Error: Unresolved compilation problem:
The method display() from the type A is not visible
Exception in thread "main"
java.lang.Error: Unresolved compilation problem:
private method is invoked
Exception in thread "main"
java.lang.Error: Unresolved compilation problem:
The constructor A(String) is not visible
child class method
Variables in method declarations—these are called parameters.,Zero or more modifiers, such as public or private.,The first (left-most) modifier used lets you control what other classes have access to a member field. For the moment, consider only public and private. Other access modifiers will be discussed later.,The fields of Bicycle are named cadence, gear, and speed and are all of data type integer (int). The public keyword identifies these fields as public members, accessible by any object that can access the class.
public int cadence; public int gear; public int speed;
public class Bicycle {
private int cadence;
private int gear;
private int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
public int getCadence() {
return cadence;
}
public void setCadence(int newValue) {
cadence = newValue;
}
public int getGear() {
return gear;
}
public void setGear(int newValue) {
gear = newValue;
}
public int getSpeed() {
return speed;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
Last Updated : 26 Nov, 2019
Output:
Public method
Inside derived class
Public method
Public method Inside derived class Public method
Traceback(most recent call last):
File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in
obj1.__fun()
AttributeError: 'Base'
object has no attribute '__fun'
Traceback(most recent call last):
File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in
obj2.call_private()
File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private
self.__fun()
AttributeError: 'Derived'
object has no attribute '_Derived__fun'
‘Although we cannot directly access private variables from outside the class, there is a way to get around this. We can define a public method that returns the value of a private variable.’,When we define a method in the constructor that is in object scope, it becomes public but being inside the constructor it is scoped to local variables, and so can retrieve what they reference and pass it up to global scope through the instance object.,A method may be public or private, just as any variable. Private methods are local to the function, like private variables. To access them we need a public helper method. You will cover this more in this unit, I believe, so I won’t wreck it for you.,A variable defined with this in its identifierName is a property of the instance object defined as taking its prototype from the constructor. These are public variables because the instance object is in global scope.
When we define a method in the constructor that is in object scope, it becomes public but being inside the constructor it is scoped to local variables, and so can retrieve what they reference and pass it up to global scope through the instance object.
john // is the instance object.
john.getBalance // is a property in object scope
// the method has both object scope and local scope
this.getBalance: function() {
return bankBalance;
}
Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects. , The visibility of a property, a method or (as of PHP 7.1.0) a constant can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inheriting and parent classes. Members declared as private may only be accessed by the class that defines the member. , Class properties may be defined as public, private, or protected. Properties declared without any explicit visibility keyword are defined as public. , Class methods may be defined as public, private, or protected. Methods declared without any explicit visibility keyword are defined as public.
string(5)
"hello"
Accessed the private method.
JavaScript typed arrays
class ClassWithPrivateField {
#privateField;
}
class ClassWithPrivateMethod {
#privateMethod() {
return 'hello world';
}
}
class ClassWithPrivateStaticField {
static #PRIVATE_STATIC_FIELD;
}
class ClassWithPrivateStaticMethod {
static #privateStaticMethod() {
return 'hello world';
}
}
class ClassWithPrivateField {
#privateField;
constructor() {
this.#privateField = 42;
delete this.#privateField; // Syntax error
this.#undeclaredField = 444; // Syntax error
}
}
const instance = new ClassWithPrivateField()
instance.#privateField === 42; // Syntax error
class ClassWithPrivateField {
#privateField;
constructor() {
this.#privateField = 42;
}
}
class SubClass extends ClassWithPrivateField {
#subPrivateField;
constructor() {
super();
this.#subPrivateField = 23;
}
}
new SubClass();
// SubClass {#subPrivateField: 23}
class ClassWithPrivateStaticField {
static #PRIVATE_STATIC_FIELD;
static publicStaticMethod() {
ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD = 42;
return ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD;
}
publicInstanceMethod() {
ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD = 42;
return ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD;
}
}
console.log(ClassWithPrivateStaticField.publicStaticMethod()); // 42
console.log(new ClassWithPrivateStaticField().publicInstanceMethod()); // 42
class BaseClassWithPrivateStaticField {
static #PRIVATE_STATIC_FIELD;
static basePublicStaticMethod() {
this.#PRIVATE_STATIC_FIELD = 42;
return this.#PRIVATE_STATIC_FIELD;
}
}
class SubClass extends BaseClassWithPrivateStaticField {};
let error = null;
try {
SubClass.basePublicStaticMethod();
} catch (e) {
error = e;
}
console.log(error instanceof TypeError);
// true
console.log(error);
// TypeError: Cannot write private member #PRIVATE_STATIC_FIELD
// to an object whose class did not declare it
class ClassWithPrivateMethod {
#privateMethod() {
return 'hello world';
}
getPrivateMessage() {
return this.#privateMethod();
}
}
const instance = new ClassWithPrivateMethod();
console.log(instance.getPrivateMessage());
// hello world