1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* Java Class example. This Java class example describes how class is defined and being used in Java language. Syntax of defining java class is, <modifier> class <class-name>{ // members and methods } */ public class JavaClassExample{ /* Syntax of defining memebers of the java class is, <modifier> type <name>; */ private String name; /* Syntax of defining methods of the java class is, <modifier> <return-type> methodName(<optional-parameter-list>) <exception-list>{ ... } */ public void setName(String n){ //set passed parameter as name name = n; } public String getName(){ //return the set name return name; } //main method will be called first when program is executed public static void main(String args[]){ /* Syntax of java object creation is, <class-name> object-name = new <class-constructor>; */ JavaClassExample javaClassExample = new JavaClassExample(); //set name member of this object javaClassExample.setName("Visitor"); // print the name System.out.println("Hello " + javaClassExample.getName()); } } /* OUTPUT of the above given Java Class Example would be : Hello Visitor */ |
what is the out put
Its written in the buttom of the example… lol
yeah
the output will be:
Hello Visitor
regards
T
hahahahaha…
my Head is crushing ..
Thank so much for this treasure. It is just simple to learn like learning to say hello to a friend.
how can u print the….. java is a “programing language”….. in java
thx, finaly i understand!!:D
I don’t get the point of lines 24-26…or 30…everything else makes sense…
Im not sure if you still need this answered, but ill answer it for others who might see this in the future date.
You cannot directly alter other class’s variable when it is set as private, so if you create an class object in the main class, and have to set the name, you can’t just do objName.name = “name”. you need to call the setName function like objName.setName(“name”);, as setName function itself is set public so anything outside of its scope can use the function freely. This is necessary when you don’t want name variable accessible freely, where many things can go wrong if you do. imagine if you have class with variables that require accurate information, but the variables are not set private and something accessed it by accident, altering it.
usefull information thank you…
hi i am new in this class java program.. can someone help me to how to ”create a program that shows my Name, age and location??”please help me