When it comes to Java programming you'll stumble upon this along your way. Here is an elementary answer to help new programmer learn how to use a getter method without the terminology or complexity of people is this field.
-
1*"How do I access a getter method?"* - `instanceOfObject.nameOfMethod()` (may require parameters depending on the method definations). – MadProgrammer Jul 23 '15 at 00:35
-
1*"How do I create two constant data fields?"* - `{public/protected/private} static final [type] [NAME] = [value]` – MadProgrammer Jul 23 '15 at 00:35
-
1You might like to have a read through [Classes and Objects](https://docs.oracle.com/javase/tutorial/java/javaOO/index.html) – MadProgrammer Jul 23 '15 at 00:36
-
`YourClass instanceOfObject = new YourClass();` `YourClass` will probably want to be `public`. Have a look at the linked tutorials – MadProgrammer Jul 23 '15 at 00:44
-
By constant data fields, do you mean class members? – deezy Jul 23 '15 at 00:48
2 Answers
By creating an accessor method (and not creating a mutator method).
public class MyClass {
public MyClass(int v) {
this.myField = v;
}
private int myField;
public int getMyField() {
return myField;
}
}
Then you can call that "getter" in some other class with an instance of MyClass.
public class SomeOtherClass {
public static void doSomething(MyClass my) {
System.out.println(my.getMyField());
}
public static void main(String[] args) {
doSomething(new MyClass(42)); // <-- for example.
}
}
- 191,680
- 20
- 149
- 239
When working with Java projects you'll stubble upon "getter" methods or "get" methods. This is how I solved my problems, by following these instructions.
If you're confused on why you should use "getter" methods follow this link.
- Note that this is for beginners and the language/format I use may (and in some cases is) not be proper.
- Note that you will also need to have a general concept of Java.
- This is for people who don't understand some of the Java terminology
- Take a look at my (example) project set up.
Package Explorer/Setup
- Project Name
- src
- (default package/package name)
- Class1.java
- Class2.java
Class 1
public class Class1 { // creates an object static Class2 class2 = new Class2(); public static void main(String[] args) { // this will print our method (method1) in our class (Class2) System.out.println(class2.method1()); } }Class 2
public class Class2 { // this is the method we are accessing public double method1(){ // this is what we are returning (sending back) return 2.5; } }Output (console)
2.5
So how do we access a "getter" method?
If you haven't noticed already, we printed it in our class "Class1" using...
System.out.println(class2.method1());
we used class2. because we created an object that allows us to access our Class2. Notice that class2 is lowercase and Class2 is uppercase, this is because class2 (lowercase) is the object we've created. Thus we are using the object to use our "getter" method not our class. We create our object using...
static Class2 class2 = new Class2();
-
Yes, you should prefer copying the code instead of "rewriting" it from the IDE. A lot of people on Stack Overflow do some mistakes during that part :P. Thanks for the fix. – Tom Jul 25 '15 at 12:28