Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

new  RSS feed

 
asin lin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a student of mechanical engineering department.
I have just studied java for a few weeks and I don't
quite understand the concept of class.
A a=new A();
would you please tell me the meaning of this codes and
when I can use it?
ps: why is java useful to a student of electrical engineering department?
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class defines the common properties and functionality avaliable to an object. "new" instantiates an instance of a class. What does that mean? It means it constructs an object of the type defined in the class. Suppose you write:

what have you got? You have two instances of the String class. Both have the same functionality avaliable to them, both have the same value, but the are seperate objects in memory.
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asin,

I have made up a very small program to show you what is happeneing.
first of all i declare a class and called it A.
the curly braces open and close blocks of code. these work in pairs.
Next I made a constructor method which gets called when I make an object that has the same arguments as the object. so in this case if i make an object of new A() i call this whole method on the 3rd line.
I made a print statement, to output what is in the " double quotes.
Next, I made a main method, this is where everything starts.
So inside this main method, I have a reference to class A, which I have called a. I have assigned this to the object of A which is essentially the constructor method. so this line of code will print what is in the print statement.
Now, I can make as many objects as I wish, but they should have different identifier names, like:
A a = new A();
A b = new A();
A c = new A();

You can also use objects to call other methods like:

Hope this helps you understand why you have the line A a = new A():
Davy
[ March 11, 2004: Message edited by: Davy Kelly ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always think of it like baking. you have a recipie for a cake, a sign saying what kind of cake it is, and a kitchen.
the recipie is the class file, or cake.java
then you leave directions for your chef to make you a birthday cake. so you say "make me a birthday cake, and use the cake recipie".
or...

later, you will learn about polymorphism and inheritance, where you could say


as to why Java is useful to EE or ME... maybe it's not Java itself, but the fact that you're gonna learn about logical thinking, how to approach a problem, analyze and break it down, etc.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!