In this part of my Java Video Tutorial I show you how to create classes in Java.
I cover Java fields (class variables) and methods (functions) first. I then explain what a private field is in detail. We then move on to what it means to overload a method, what a constructor is and how to overload a constructor.
Don’t worry. It sounds much more complicated than it is. You should however check out the previous tutorials before you try to learn this stuff here Java Video Tutorial.
If you like videos like this share it [nttfblike]
Code From the Video
Great video series for java ,Thank You Very Much sir for this videos,I’ve learnt java accurately by your video series.
I’m very happy that you liked it. I have many ore Java tutorials coming. I hope you like them
hi,this was so helpful…
but i have problem with the ” passing arrays to methods” concept.Here is the code which i tried .
hope u will reply soon
import java.util.*;
import java.io.*;
class Arr
{
int r,c;
int arr[][];
Arr(int r,int c)
{
this.r=r;
this.c=c;
arr=new int[r][c];
}
int[][] getMatrix() throws IOException
{
Scanner s=new Scanner(System.in);
for(int i=0;i<r;i++)
{
for(int j=0;i<c;j++)
arr[i][j]=s.nextInt();
}
return arr;
}
int[][] sum(int a[][],int b[][])
{
int q[][] =new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
q[i][j]=a[i][j]+b[i][j];
}
}return q;
}
void displayMatrix(int k[][])
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.println("the resultant matrix is:"+k[i][j]);
}
}
}
}
public class To
{
Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.println("enter no of rows:");
int r=sc.nextInt();
System.out.println("enter no of columns:");
int c=sc.nextInt();
Arr a=new Arr(r,c);
Arr b=new Arr(r,c);
int x[][],y[][],z[][];
System.out.println("enter matrix a:");
x=a.getMatrix();
System.out.println("enter matrix b:");
y=b.getMatrix();
z=a.sum(x, y);
b.displayMatrix(z);
}
}
Is this the error you’re getting java.lang.ArrayIndexOutOfBoundsException
yes…………
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5
at Arr.getMatrix(To.java:22)
at To.main(To.java:74)
this means that you or your program specified a field that doesn’t exist in the array
yes ………..
i replied twice…..but don’t know what happened
Is there something special that needs to be done to get this to work on NetBeans?
Great vids by the way.
Thanks
Thank you very much 🙂 No everything should work on NetBeans without an issue. On occasion I import a library, but aside from that everything should work perfectly
Class variables/fields are different than private fields? I’m trying to make this work in my brain. All monsters will have a TOMBSTONE I follow that. Then you have the health, attack, movement, x and y position and alive boolean listed next as private fields. Why are they not also final? If the word private means they are treated that way then why not privatize the TOMBSTONE? I think I’m stuck on the difference between each and all. All monsters will have a TOMBSTONE when they die but each monster has health… is that the difference?
I grew to dislike the idea behind this game after I made it. I mainly focus on giving examples of all the different ways you can use the different tools in Java. I think this pseudo game just got in the way. Feel free to ignore that part of the videos because I very quickly get off this topic and move into better videos very soon. I’m sorry if it was confusing
Hey Derek just wanna say thank you for taking the time to make the vids, i am currently doing programming at university and i am having a lot of trouble understanding the methods and the classes, no matter how many times i watch them i cannot get my head around the idea. What your doing there in the monster game i been trying to follow along by programming as well but there is a lot of stuff i don’t get, if you dont mind clearing some of the things up can you please add me on skype trujohn3665 thx
The easiest way to send me questions is actually through my website, or through YouTube. I check YouTube instant messages normally every 2 hours. I will gladly answer any questions you’d like. I have tried chatting and it normally doesn’t work for some reason. Just make a list of questions and I’ll answer them
Hi Derek,
I really love your videos. I first came across your videos while doing python. Your python videos are fantastic. I have an exam next week on java so I’m watching your videos day and night. I’m having a problem understanding the toString method. Can you give me some advice.
Every class you create is of type object. So, every class you create comes with the toString method by default. When your code tries to print an object it will print to screen whatever you define in toString. So, by defining it you can decide what prints out. Does that help?
Firstly, thanks for your reply. You have given me some hope. I understand somewhat but can you give me a bit of code showing how it is used. I don’t quite understand the syntax. Thanks again.
You’re very welcome 🙂 Sorry, but I’m not sure what code you are looking for. I get tons of questions and I forget past conversations some time. Sorry about that
Hi again Derek,
Right now you are my lifeline for passing my exam on Friday. I’m trying to figure out how to print the values in an array using the toString method. I welcome any suggestions or comments.
Thanks(an avid fan)
Allan.
Hi, Create a toString method in a java class and override the default. The array would be part of that class. Use a for loop to iterate and return the results in toString. I hope that helps 🙂
Why can’t public String access the private int (attack) but in public void you can?
You can change public fields by just referring to them, but to change a private field you must do so through a method set up to provide that capability
Hi,
I appreciate the work you do here. I am learning, slowly but surely. One thing I applaud you greatly is the fact that you take time and respond to the questions and comments. I admire you for that. Thanks for your effort!
I do my best. Sometimes I get a little overwhelmed, but I get to all of them eventually.
by the way, which is this your website or do you have another website? Just want to know where you prefer to be contacted for questions or comments.
This is the only website I use for tutorials.
I just wanted to say that you are doing an AMAZING job,your youtube channel is the best channel I’ve ever seen.
Thank you very much 🙂 I appreciate you taking the time to tell me.
Hi Derek on line 70 where it says the method has the same name as the class, is that supposed to say the constructor has the same name as the class????
Yes sorry about that
there is a mistake in the youtube video. at 7:10
the paramater in the video is written as the follow.
public void setHealth(double decrease)
instead of
public void setHealth(double decreaseHealth)
Sorry about that