0

Having trouble with objects in java

Ive learnt how to create an object But am not able to understand how to assign attributes to it (what does that "." Mean ) , What is static keyword and how is it related to objects , And there r a lot more things I don't understand about objects , can someone explain me with a better example (than in solo learn chapters) , the example has 'customer' as both class name , object name and method name so it's confusing Someone please 🙏🙏

26th Oct 2022, 12:25 PM
kevin paul
kevin paul - avatar
5 ответов
+ 4
A sample take : class Student { String name; int id; double percentage; // let have some methods: public void display() { System.out.println( name + "\n" +id+ "\n" + percentage) ; } } For this class, you can create an object by : Student st = new Student(); // we have no constructor added, no having default empty parameter constructor. Now st is our object name of class Student. by using dot(.) , you can access properties like : st.name = "John"; st.id = 1; st.percentage = 89.0; You can call methods also like : st.display() ; // will call method and prints output : John 1 89.0 You can change values like : st.percentage= 90; You can access property like : System.out.println( st.percentage) ; May be in lesson, class name is Customer; (caps C). object name is customer (Small c). Capitalization of c is different. hope it helps...
26th Oct 2022, 1:12 PM
Jayakrishna 🇮🇳
+ 4
1) where ever you need. Mainly it's done other classes, especially in main class. 2) Same answer. Where ever you need.. Suppose you have get method getId(), then you can call it in display method like public void display() { // System.out.println( getId() ) ; // } If not understood, then don't complicate it now.. Your expections are correct. But there are other possible also, as I mentioned. Scanner sc = new Scanner(System.in); // sc is an object of Scanner class. And here System.in is parameter passing to Scanner class constructor. It is object of System class input stream.. Scanner class has a constructor Scanner(InputStream) . i.e it will let you to pass the object of InputStream class. Here "in" is an object of "InputStream" class defined in System class like "out" is an object of PrintStream class defined in System class) tells the compiler that input is provided through keyboard. Let's take another case, if you pass a file name, then it means input is provided from the file..
26th Oct 2022, 1:49 PM
Jayakrishna 🇮🇳
+ 2
Ok , thanks I did not understand the scanner class part though Maybe because I did not come across constructors I hope I'll understand this when I complete constructors
26th Oct 2022, 1:59 PM
kevin paul
kevin paul - avatar
+ 2
> how to assign attributes to it class A { int attribute = 0; } // attribute has assigned default value ... A a = new A(); a.attribute = 10; // other way > (what does that "." Mean ) attribute that is member of object a > What is static keyword and how is it related to objects , static field has same value to all instances class B { static int common = 0; } B b1 = new B(); B b2 = new B(); B.common = 100; //assigned direct to class, not object System.out.println( b1.common); //100 System.out.println( b2.common); //100 > both class name , object name and method name so it's confusing yes it can confuse, but compiler understands it, this is possible but not recommended: class Program { Program() {} // constructor void Program() {} // method ... Program Program = new Program(); Program.Program();
26th Oct 2022, 2:06 PM
zemiak
+ 2
class A { int attribute = 0; } class B { static int common = 0; } public class Program { Program() {} // constructor void Program() {} // method public static void main(String args[]) { A a = new A(); a.attribute = 10; System.out.println( a.attribute); //10 B b1 = new B(); B b2 = new B(); B.common = 100; // assigned direct to class System.out.println( b1.common); //100 System.out.println( b2.common); //100 Program Program = new Program(); Program.Program(); } }
26th Oct 2022, 2:06 PM
zemiak