Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to define and use a simple class with fields, constructor and method in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

        private String name;
	    private String address;
 
	public JavaApplication1(String _name, String _address) {
		name = _name;
		address = _address;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public void setAddress(String address) {
		this.address = address;
	}
 
	public void showDetails() {
		System.out.println("Hello my name is " + name + " I'm from " + address);
	}
    public static void main(String[] args) {
        JavaApplication1 person = new JavaApplication1("Dan", "LA");
		person.showDetails();
                
        person.setName("Ron");
        person.showDetails();
    }
}

/*
run:

Hello my name is Dan I'm from LA
Hello my name is Ron I'm from LA

*/

 



answered Jan 5, 2016 by avibootz
edited Jan 5, 2016 by avibootz
0 votes
package javaapplication1;

public class JavaApplication1 {

        private String name;
	    private String address;
 
	public JavaApplication1(String _name, String _address) {
		name = _name;
		address = _address;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public void setAddress(String address) {
		this.address = address;
	}
 
	public void showDetails() {
		System.out.println("Hello my name is " + name + " I'm from " + address);
	}
    public static void main(String[] args) {
        JavaApplication1 person1 = new JavaApplication1("Dan", "LA");
		person1.showDetails();
                
        JavaApplication1 person2 = new JavaApplication1("Ron", "CA");
        person2.showDetails();
    }
}

/*
run:

Hello my name is Dan I'm from LA
Hello my name is Ron I'm from CA

*/

 



answered Jan 5, 2016 by avibootz

Related questions

1 answer 248 views
1 answer 259 views
259 views asked Jun 28, 2015 by avibootz
1 answer 295 views
1 answer 197 views
1 answer 252 views
1 answer 295 views
1 answer 469 views
...