Skip to main content
4 answers
4
Asked 608 views

Java Programming - Constructors in Java?

what are the importance of learning Java programming in this century?

what is the real world example of constructors in programming languages and how can it be explained literally to a newbie?

+25 Karma if successful
From: You
To: Friend
Subject: Career question for you

4

4 answers


0
Updated
Share a link to this answer
Share a link to this answer

Jimil’s Answer

Frankly, most companies use Python, C++, C#, Javascript, or GO nowadays. Java isn't used as much, and it is less applicable in the real world, even though it is taught in many schools as an intro class for programming. The other languages perform similar functions, just written a bit differently and they are more easily used for certain applications. Most languages are 20+ years old, but the key factor is that the other languages are used more commonly for their respective purposes. Watch some youtube videos or do some codeacademy or udemy courses to learn them. Or make a project with a goal in mind.

Constructors in Java: A constructor in Java is a special type of method that is used to initialize an object when it is created. Constructors are called automatically when an object is created, and they do not have a return type, not even void. The purpose of a constructor is to set the initial state of an object. In Java, constructors can have different types of parameters and can be overloaded, which means you can have multiple constructors with different parameters.

Importance of learning Java Programming: Java is one of the most widely-used programming languages in the world and is used in a wide range of applications, such as web development, mobile apps, enterprise software, and big data processing. Java is also a versatile and platform-independent language, meaning that Java code can run on any platform that has a Java Virtual Machine (JVM) installed, this makes it a valuable skill for a number of industries such as finance, healthcare, retail, and transportation among others.

Real-world example of constructors in programming languages: A real-world example of constructors can be a class that represents a car. In this class, you might have several constructors, one that creates a car with just a make and model, another that creates a car with a make, model, and color, and a third that creates a car with all of the above plus additional options like sunroof, navigation system and others. Each time you create a new car object, you would use the appropriate constructor to set the initial state of the car object with the correct make, model, color, and options that you want.

In simple terms, constructors in programming languages are like a blueprint or a recipe for creating an object, it's like a set of instructions that are followed automatically each time an object is created, that ensures that the object is in the correct initial state.

0
0
Updated
Share a link to this answer
Share a link to this answer

Peter’s Answer

Fred provide lots of detail on the constructor, but I'm curios why you asking this question. are you learning java and wonder why you need to have a constructor? as most languages that have an object orient concept, there is a form of constructor, so pick up a book on objective oriented programming, then the purpose of constructor should become clear. as for java, you'll not got far as a programmer if you do not understanding the constructor.
0
0
Updated
Share a link to this answer
Share a link to this answer

Fred’s Answer

If you are not going to be a programmer, or work with computers, knowing java isn't really important. A physician will (generally) not need to know it, just like a programmer does not need to know the details of human anatomy (there are always exceptions, of course). So in that aspect, there is no importance if you don't want to program.

A constructor is a way to instantiate an object with required and allowable values. If i were creating an instance of a House object, one possible parameter may be "master_bedroom_wall_color". This might be a string, but I probably don't want to allow a string value of "fred is a good programmer". My constructor would enforce only valid values go in there. Further, my construction company may only allow two choices - white or eggshell. My constructor could enforce that. Or if someone makes a different choice, maybe it increases the price to build the house.

A constructor also enforces that all required fields are populated. it makes no sense to build a house without an address (or some kind of location).

Side note: this is probably not the best site to ask technical questions like "What is a constructor?" This site is more focused on college/career type question. For technical questions, you'd be better off at a site like coderanch.com (disclosure - I am a moderator there) or Stack Exchange (although they can be intimidating towards newbies).
0
0
Updated
Share a link to this answer
Share a link to this answer

Tony’s Answer

Patrick, if you are asking "Should I learn Java to get a job in computer programming?" It is not a bad language to learn, and you can learn the basics of programming, including object-oriented concepts. Java runs on Windows, MacOS and Linux, so easy to try out on your existing desktop or laptop computer.
https://www.golinuxcloud.com/install-java-linux-windows-mac/

Generally, each job posting will mention what programming language is used for the product you will work on. According to this website, Java is #2 programming language behind Python, and the average U.S. salary of Java programmer is between $75,000 to $100,000 per year.
https://skilldeck.org/learning/best-programming-language-to-learn/?gclid=CjwKCAiAjs2bBhACEiwALTBWZS81L4F3VPeO9etODfLRGySmVpoRgJ0qtlJFvIPXDp5G0XZnZ6Fu6hoCLLsQAvD_BwE

Please note that Java and JavaScript are two different programming languages. Do not confuse one with the other.

Tony recommends the following next steps:

Install java on your computer: https://www.golinuxcloud.com/install-java-linux-windows-mac/
0