My first Java program

2 replies
public class Student1{
int id;
String name;
Student1(int i,String n){
id=i;
name=n;}
public String getName(String n)
{return name;}
public static void main(String args[]){
Student1 s1=new Student1(3,"Sergiu");
System.out.println(s1.getName(""));
}
}

This is what came out after dozens of tries trying to learn about the syntax, constructors and getters. How would you do the program in a simpler way?
The goal is to define a Student object in main and to output one of its parameters.
PS: I'm ready to become a programmer, guys!
#java #program
  • Profile picture of the author JMondaCom
    PHP Code:
    public interface Student{
        
    String getName();
    }

    public class 
    Student1

        private 
    Integer id;
        private 
    String name;

        public 
    Student1(Integer i,String n){
            
    this.id i;
            
    this.name n;
        }

        public 
    String getName(){
            return 
    name;
        }

        public static 
    void main(String[] args){ 
            
    Student s1 = new Student1(3,"Sergiu");
            
    System.out.print(s1.getName());
        }

    You have to work with Integer in class because is a object
    {{ DiscussionBoard.errors[10792769].message }}
  • Profile picture of the author minirich
    No, you don't have to use Integers.
    In the latest Java versions there is autoboxing implemented. If you try to put an Integer into an int or vice versa, the JVM will do it automatically for you without complaining.

    It is correct that Integer is an object and in many cases it makes sense to use an Integer instead of an int.

    @hetingson its pretty much the minimum you have to write for what you want to accomplish
    JMondaCom formated it nicely.

    Have a look at IDE's (Eclipse, IntelliJ IDEA,...) or advanced text editors (JEdit, Notepad++,...) with Syntax highlighting for Java, it makes life easier.
    IDE's are even more helpful, as they would instantly point out if you have an syntax error or a typo in a variable name.

    They also have code completition, which speeds up coding alot.
    Almost all of the IDE's have a built in lookup for functions and methods of library classes and show you the javadoc of them too.

    Javadoc is the Java documentation format, it is maintained directly in the code next to the methods you write.

    Cheers Mike
    {{ DiscussionBoard.errors[10815772].message }}

Trending Topics