0% found this document useful (0 votes)
3 views

Structure of a Class file

Java coding help

Uploaded by

marthaghinrichs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Structure of a Class file

Java coding help

Uploaded by

marthaghinrichs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

public class MyClass {


2. // Declare class (static) variables here
3. public static int foo = -5;
4. public static String bar = "HelloWorld";
5. public static boolean isFoo = false;
6.
7. // Declare instance variables here
8. public double variable1;
9. public String variable2;
10. public int[] list1;
11.
12. // CONSTRUCTOR
13. public MyClass() {
14. this.variable1 = 0.0;
15. this.variable2 = "nothing";
16. this.list1 = new int[]{1, 2, 3, 4, 5};
17. }
18.
19. // INSTANCE METHOD
20. public double instanceMethodExample(){
21. return this.variable1;
22. }
23.
24. // INSTANCE METHOD
25. public void anotherInstanceMethod() {
26. this.variable2 = "not nothing";
27. }
28.
29. // STATIC METHOD
30. public static void staticMethodExample() {
31. System.out.println(foo);
32. }
33.
34. // STATIC METHOD
35. public static String anotherStaticMethod() {
36. return bar;
37. }
38.
39. // MAIN METHOD
40. public static void main(String[] args) {
41. // To call a static method
42. staticMethodExample();
43. String myFoo = anotherStaticMethod();
44.
45. // To create a new instance of a class, call the constructor
46. MyClass obj1 = new MyClass();
47.
48. // To call an instance method on an instance of a class
49. obj1.anotherInstanceMethod();
50. double obj1_var = obj1.instanceMethodExample();
51. }
52. }

You might also like