1 Replies - 415 Views - Last Post: 19 May 2010 - 05:46 PM Rate Topic: -----

#1 n3xtgen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 19-May 10

New to Java. Confused about Classes

Posted 19 May 2010 - 05:27 PM

Hello everyone. I started to learn java 3 days ago and so far everything is going good. I started to read on today about Classes, but am having a very hard time understand the NEW function that is used. For example, this is a copy of some code I was working along with a video example:

public class MyClass {

	int myClass_properties;
	void setMyClass_properties (int number) {
	myClass_properties = number;
	}

	int getMyClass_properties () {
	return myClass_properties;
	}
	public static void main (String args []) {
	  MyClass myClass;
	  myClass = new MyClass ();

	  myClass.setMyClass_properties (5);
	  System.out.println("Result " + myClass.getMyClass_properties());
	}

}


The following code I do not understand:

	return myClass_properties;


	  MyClass myClass;
	  myClass = new MyClass ();

	  myClass.setMyClass_properties (5);


What is exactly going on here? What does the return do?
Also, what is the MyClass myClass declaration and the myClass = new MyClass ()? I am confused about the keyword new and the empty (). What is it creating?

Thank you :smile2:

Is This A Good Question/Topic? 0
  • +

Replies To: New to Java. Confused about Classes

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10826
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: New to Java. Confused about Classes

Posted 19 May 2010 - 05:46 PM

The MyClass obj; declaration declares a new pointer of type MyClass. Then when you instantiate your object obj = new MyClass();, you are telling that pointer to point to a new Object in memory of type MyClass, as well as creating the new Object. The MyClass() is the constructor method, invoked when you create a new instance of an Object.

The return statement returns the value stored in myClass_properties to the invoking line. So for example"
myClass.setMyClass_properties(5);
int x = myClass.getMyClass_properties(); //x = 5


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1