0

I have a class I created.

public class mailCustomer {
    public static void main(String[] args) {
        String Name;
        int Id;
        String Address;
        Boolean IsPack;
    }
}

I need to creat get and set methods for my parametrs, Name, Id,Address, IsPack. how do I do that, and where do I write them? after the "main" method? in the "main" method?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I would familiarize yourself more with the language. But how you would do it is you need to declare those variables outside the main what we call `globally` and then just for the heck of it put `private` in front of them. Then create the methods for them – 3kings Nov 06 '15 at 14:28
  • 2
    try readin this first http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work – bakki Nov 06 '15 at 14:29
  • This is widely covered in tons of tutorials, even on [official Java tutorials](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – Luiggi Mendoza Nov 06 '15 at 14:36

4 Answers4

1

Firstly, you need to declare the variables at the class level so that they can be used from anywhere within the class. Then after that you simply create a set and get method for each variable, like so

public class MailCustomer {
    String name;
    int id;
    String address;
    boolean isPack;

    public static void main(String[] args) {

    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setIsPack(boolean isPack) {
        this.isPack = ispack;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public String getAddress() {
        return address;
    }

    public boolean getIsPack() {
        return isPack;
    }
}

It also appears that you have your naming conventions a little mixed up. In java, class names are capitalized, whereas variable and method names are camelCased

Brandon Laidig
  • 430
  • 2
  • 14
1

First, delete the main method from there. You are creating a class called mailCustomer that will create you object type of mailCustomer.

For this, you need three things: attributes (you have them there), a constructor and get/set methods. I will show you with a example, and you can guide from there. My class will be 'Rabbit' and it will have:

Attributes

-String eyes: Eye colour.

-String race: Where is it from.

Constructor

It will have one constructor, that will use both parameters.

Methods

It will have two getters and two setters, both for each attribute.

Here's my code for this class:

public class Rabbit{

   //Attributes

   private String eyes;
   private String race;

   //Constructor

   public Rabbit(String colour, String where){

   this.eyes = colour;
   this.race = where;
   }

   //Methods get/set

   public String getEyes(){

   return this.eyes;
   }

   public String getRace(){

   return this.race;
   }

   public void setEyes(String colour){

   this.eyes = colour;
   }

   public void setRace(String where){

   this.race = where;
   }

}

As you can see, you will use get methods to return an specific attribute from the class; and set methods will be used if you want to change one of the attributes from a created object (in my case, from a created 'Rabbit').

Later, if you want to make use of this class, you will create your Main class, into the same package where 'Rabbit' class is created.

package rabbit;

public static void main(String[] args){

Rabbit George = new Rabbit("brown","spanish");

}

Now try to do this with your class. I hope it helped you!

xFunkyTImes
  • 117
  • 9
0
public static void main(String[] args) {

    String Name;
    int Id;
    String Address;
    Boolean IsPack;

}

You can't create getters and setters for these, since they are not created in your class, but locally in your main method. What you want is:

public class MyClass{

String Name;
    int Id;
    String Address;
    Boolean IsPack;

// getter for Name
public String getName(){
  return this.Name;
}
// setter for Name
public void setName(String name){
  this.Name = name;
}

public static void main(String[] args) {

}
}

For these you'll be able to create getters and setters. It's (most likely) best to declare them private, though. Also: following naming conventions could help other people easier read your code. Name (with capital N) is more suited as name of a class, while name (lowercase n) is the name of a variable.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • Why the downvote, if I may ask? – Stultuske Nov 06 '15 at 14:30
  • 1
    I didn't down vote but i'm just saying you maybe want to show an example of a setter/getter method and how he would be able to do it. +1 from me otherwise – 3kings Nov 06 '15 at 14:32
  • 1
    Good point, I've updated it with a getter and setter for Name (and a recommendation for following Naming conventions) – Stultuske Nov 06 '15 at 14:35
0

As already said it seems that you are not familiar with Object Oriented Programming. It does not care. I just want to say that you can save a lot of time and improve your classes readability using lombok to automatically generate your getters and setters using annotations. You can find an example here : LOMBOK