FAQ Database Discussion Community


Passing to Setter and Getter methods

java,methods,inner-classes,setter,getter
I have a Bread class and a Filling class which set the bread type and calories per slice as well as a filling class which sets the filling type and calories per serving... I can't seem to figure out how to pass them into the sandwich class correctly. My total...

How to call a method of an object that was instantiated in another class?

java,arrays,object,methods,getter
Given the three example classes: public class MainClass { public MainClass() { ArrayOfObjectA arrA = new ArrayOfObjectA(); } } , public class ArrayofObjectA { ... public void doSomething() { ... } } , and public class ObjectA { ... public void doThis() { arrA.doSomething(); // <-- Here's the issue }...

Java: How do I create a getter method that gets an object from an inherited class?

java,class,inheritance,getter,getter-setter
I'm trying to form a getter method of an object Mammal within a class Catalog. The get method has no parameters, and has a return type Mammal (?) public class Mammal extends Animal { private double intellect; private double hitpoints; public Mammal(String firstName, String lastName, String nickname) { super(firstName, lastName,...

Getter and Setter Function Help and How To

java,function,setter,getter
I am currently writing a program in which I need to incorporate get functions to produce and output for the program. The program needs to have Number of SeatsA = 30 and the pricePerSeatA = 120.99. I thought the way my program was set up all i would need to...

How comes properties act like functions?

javascript,properties,getter
If you install colors you will see you can write scripts like this: var colors = require('colors'); console.log('hello'.green); console.log('i like cake and pies'.underline.red) console.log('inverse the color'.inverse); console.log('OMG Rainbows!'.rainbow); console.log('Run the trap'.trap); How is is possible that properties behave like functions (like [5, 6, 4].count?). I understand 'Run the trap'.trap() but...

How to access an Arraylist of another class in java

java,arrays,class,arraylist,getter
I know that it might be a basic question but the codes that i got for this question does not work on my arraylist. May be someone could help me solve this.I would like to access the arraylist "Connection" of class Testing1.Java and use it in another class named SQL.Java....

Guarantee not optional value, while allowing optional value as setter

swift,setter,getter,optional
class MyClass { private var _image: UIImage var image: UIImage { set { if newValue == nil { _image = UIImage(named: "some_image")! } } get { return _image } } } My goal is to guarantee non-optional value when image is accessed Can i achieve this without additional function? Even...

How to use setters and getters in a Javascript object (Class)?

javascript,class,setter,getter,getter-setter
The following script DOES NOT WORK. What would be the proper way? function AnObject () { get this.apple = () { return this.apple + "GET"; } set this.apple = ( newValue ) { return newValue + "SET"; } } var obj = new AnObject(); obj.apple = "Lemon"; console.log( obj.apple );...

Using one getter with an argument as opposed to multiple getters with no arguments

java,oop,getter
My first attempt at doing some OOP that isn't simple practice exercises is to make a program in java which stores information about people (like a contact book). Below is a snippet of a class called Person which I'm working on. Person will be instantiated by some other class some...

Check if class property has been set

c#,equality,setter,getter
Lets assume I have a User class public Class User { public string Name { get; set; } public string Surname { get; set; } public int Level {get;set;} } User user1 = new User(); user1.Name = "name"; user1.Surname = "Surname"; user1.Level = 0; User user2 = new User(); user2.Name...

Access objects variable & method by name

java,class,oop,getter
Example class: class Test { public int marks; public String location; public String show(){ return "Good morning!"; } } Is there a way I can access marks and locations by a getter? Maybe like so: Test t = new Test(); System.out.println(t.get("marks")); and maybe System.out.println(t.call("show")); The exact use case I have...

How to protect private field in class with getter in java

java,getter
I have the following code. public class GetterTest { public static void main(String[] args) { Car car = new Car(); StringBuilder wheel = car.getWheel(); wheel.append("asd"); System.out.println(car.getWheel()); } } class Car { private StringBuilder wheel; public Car() { wheel = new StringBuilder("a"); } public StringBuilder getWheel() { return wheel; } public...

Are getter and setters ok in android if using it in another class?

java,android,android-intent,setter,getter
I read that getter and setters are not good for android programming when using the method in the same class it was created. But would there be any negative effects if using it in another class? I was told not to use many static variables so i'm looking for an...

accessing getter properties from objective-c header in swift

objective-c,swift,getter
In the objective-c header we have a property like so: @property (nonatomic,assign,getter=isMyThingEnabled) BOOL enableMyThing; In swift any attempt to access this property like so generates an error that the property is not found: myClassInstance.isMyThingEnabled So how do I access the property isMyThingEnabled in swift?...

“More functional” “accessor” for collections in C#

c#,collections,setter,getter,accessor
Now I have a collection: Dictionary<string, List<string>> dictionary. For some reason, now I want to "project/map" a part of it to other collections, sort of like using it as backing field, and create different accessors for this collection. Like List<string> keys corresponds to the keys of this dictionary, or Dictionary<string,...

Better practice for heap object getters / setters in C++

c++,setter,getter
I'm currently having Type1 &GetType1() const { return *this->type1; } void SetType1(const Type1 &type1) { *this->type1 = type1; } and in the class definition class Type2 { public: Type2(); virtual ~Type2(); Type1 &GetType1() const; void SetType1(const Type1 &type1); private: Type1 *type1 = nullptr; } And in main int main() {...

Java Get and then remove from a list

java,list,setter,getter,instances
In my code i have a list of instances of a class. And i want to get a attribute of 1 instance which is ArrayList. In this class i have implement getters and setters. So I call listofinstances.get(i).getArrayList().remove(0); in order to remove the 1st item of this list. Is this...

org.hibernate.PropertyNotFoundException: Could not find a getter for id in class model.Contact

java,hibernate,netbeans,getter
I am trying to learn Hibernate and I am following this tutorial: http://www.visualcplusdotnet.com/javaopensource/javahibernateswingdesktopapp5.html except I am using my own sample database. I have reverse engineered the POJOs for my contact table which looks like this: and Netbeans auto-generated a .java file that looked like this (Contact.java): package model; // Generated...

Delphi Classes: Property vs Get/Set methods

class,delphi,properties,setter,getter
So I'm kinda new to OO programming. Delphi has properties which are intended to be a "more elegant" way to acess class data than getters/setters (read it here usage of property vs getters/setters in business classes). When should I use the fields directly and when should I use getters/setters inside...

Java getText of looped multiple JTextFields

java,for-loop,jtextfield,user-input,getter
My first question here. Already got a lot of help but now I don't know how to do. My code: package view; import javax.swing.*; public class OptionPlayerNames { JPanel playerPanel = new JPanel(); JTextField playerNames = new JTextField(); public OptionPlayerNames() { for (int i = 0; i < 8; i++)...

getters() and setters() simplification for a class

java,encapsulation,setter,getter
Why do we exactly need to use the set and get methods in our class that use private attributes? When it is really used during our program process? Can we still make it work without it (without changing the private attributes to public)? This is an example of a...

Using Getters and Setters: a Beginners guide

java,setter,getter
Okay so I'm totally a beginner and just learning this in class. Can anyone tell me what I'm doing wrong? When I run this code it's supposed to tell me that myMonkey and myFavoriteMonkey are the same color... but I keep getting a null value for the output... I've got...

Swift structures properties is the same type as structure it self. Recursion?

swift,structure,static-members,getter
Please help me to understand the following Swift construction: struct UIViewAnimationOptions : RawOptionSetType { init(_ rawValue: UInt) init(rawValue rawValue: UInt) static var LayoutSubviews: UIViewAnimationOptions { get } static var AllowUserInteraction: UIViewAnimationOptions { get } static var BeginFromCurrentState: UIViewAnimationOptions { get } static var Repeat: UIViewAnimationOptions { get } static var...

Access to class variable

c++,polymorphism,getter
I've got a problem with access to an object, which is a variable of a class. The inheritance and relationship looks as follows: #include "X.h" class Y { X object; public: X & getObject(); }; Then another file/class: #include "Y.h" class Z : public Y { X object2; public: X...

Create multiple properties with the same getters and setters

javascript,setter,getter,getter-setter
The problem Recently I've been writing some JavaScript program which involves the use of getters and setters. I read the MDN documentation for both methods, but I got confused trying to use them. In a nutshell, I just want to create a series of similar properties, which will have the...

How do getter methods work on arrays?

java,getter
public class foo { private int a[]; private int b; public foo(){ a = new int[] {1,2}; b= 3; } public int[] getA(){ return this.a; } public int getB(){ return this.b; } I noticed that it's possible to change a value of A by accessing the object like this: foo...

Getter to a specified element of a vector

c++,vector,iterator,getter
I have a class as following : class Graph { private : vector<Graph*> children; Board* tab; [...] public : vector<Graph*> getchildren(); Board* gettab(); [...] } Board* Graph::gettab() { return this->tab; } vector<Graph*> Graph::getchildren() { return this->children; } I need to do a loop that reach every tab inside children (in...

Should I use Point.x or Point.getX()?

java,field,point,getter
I have a Point. I am trying to get x as an int. If I use Point.x, I will get x as an int. But I am under the impression that I should be using a getter whenever possible (Why use getters and setters?). The issue with Point.getX() is that...

Objective-C Base Class Property Custom Getter Not Being Called From Subclass

objective-c,inheritance,properties,getter
Base Class Interface: @interface Base : NSObject @property (nonatomic, readonly, getter=getPriceForListing) double_t priceForListing; @end Base Class Implementation: @implementation Base -(double_t)getPriceForListing {     if (self.listPriceLow > 0 && self.listPriceHigh > 0)     {         return self.listPriceLow;     }     else if (self.listPriceLow > 0)     {...

c++ prevent changing reference through getter

c++,class,reference,getter
I am currently learning c++ and I am an experienced C# and Java developer. I have a class B which contains a member of class A, and I want users of class B to be able to change values in class A but not be able to change the instance...

Getter and Setter in C# [on hold]

c#,setter,getter
I'm just playing around with C# and I'm aksing myself which the proper method is for Getter and Setter. I found something like this with google: class MyClass { Button btnMyButton; // some code... public Button getBtnMyButton { get { return btnMyButton; } } } is there a 'proper' way?...

Can I have just getters and not setter?

groovy,default,setter,getter
I am quite new to groovy, and I have found out that by making a field public, groovy provides getters and setters by default. Is there a way to have just the getters but not the setters by default? The reason behind this is that I have a Builder and...