Sir, I am a CSE final year student. I am learning Java from your videos. I feel very much comfortable to learn from your lectures and practical demos. Thank you so much for the clear explanations and apt examples. I have a book titled 'How to crack Interviews in Java' written by Rashmi Kanta Das. It has only questions and one word answers without explanations. Can you please upload videos with explanations for the answers in that book?
Excellent tutorial sir. this is what I had been searching for so many days. Perfect explanation and you explain so nicely that anyone who are really new to Java can also understand. This is a real compliment from my heart. Keep up the work. You are doing a great job helping out young engineers like us learn and get ourselves mastered with Java.
Hi Naveen Sir I have a few questions for you so could you please answer them through your tutorials? I am certainly not judging your knowledge in this subject but Just requesting you. 1. Create a method which can take any kind of generic parameter. For instance: map, map, List. 2. Can we use comparable in the catch block ? if so, then what would be the scenario? 3. Can we use Object class in place of Throwable as it is a parent class? 4. What the are the fastest map used in order to store billions of data? Regards
One Mistake: Sir, You changed 'i' as static variable. So that can't be used in non static method sir. We have to put static in that class method sir. Otherwise all your videos are really awesome sir. I completed your Python tutorial. Now I'm going with Java. After Java Script. Thank you so much for your videos sir.
That is not a non static method.That is a constructor. Public A() is a constructor.And constructor loads everytime an object is in play. If you are talking about that counter method which is non static then you are wrong.A non static method can access static variables or class variables.The vice versa is not possible.
dear sir, i have a doubt.. please clear variable 'i', being a non-static variable can't be used inside the count() as well as main() (as you told in the previous lectures), so it is obvious that it will not be compiled at first place and show error if we not make it static
public class A { static int i=0; A() { i++; } public static void main(String[] args) { A obj0=new A(); A obj1=new A(); A obj2=new A(); A obj3=new A(); System.out.println("Number of objects created:" +i); } }
There is some simple mistakes in code. 1) public static void counter ( instead of just public void counter) 2) in main A.counter(); (Last line instead of A obj.counter(); ) Thanks 🙂
sir in one of ur video "use of static block" u said constructor don't work on static variables but in this video its contradictory........ plz explain this..... waiting for an early explanation....
Actual use of static block : We can assign values (initial values) to static variables inside the static block similar to the way we assign values to non-static variables inside constructor. We can also assign values to static variables from constructor. But, the value will be assigned only after the first object of the class is created. And the same code (assignment statement) will be executed unnecessarily every time we create an object of that class.
// INTERVIEW QUESTION :- HOW TO KNOW HOW MANY OBJECTS ARE CREATED FOR AN CLASS? // SOLUTION: THERE IS AN CODE WHICH WILL FIND HOW MANY OBJECTS ARE CREATEING FOR TARGET CLASS. public static void main(String[] args) { target obj1=new target();//create object1 target obj2=new target();//create object2 target obj3=new target();//create object3 target obj4=new target();//create object4 target obj5=new target();//create object5 target obj6=new target();//create object6 target obj7=new target();//create object7 obj2.counter(); } } class target { public static void main(String[] args) { }// main method under this target class static int c; // class variable (by default any integer has value 0) public target() // constructer(which is create by default when we make any new object) { c++;//use to count } public void counter() // static use because the class variable is static { System.out.println(c);//print the counted value } }
hi, Telusko , is there a way to use reflection to grt the count? here it is going to only show the instances created, but what abt instances no longer present because of GC.
Raounaq Sharma bro we can have a static variable inside a non-static method but it is necessary to have a static variable with static method..i hope this will clear your doubt
Inside non static method we can use static variable..i.e [public void counter()] is correct....but we can't use static variable inside a constuctor... right?
hello sir i really like your videos and they are very simple to understand also.....but sir i have recently given TCS interview and they asked me a question about an array ...How will you tell that the two array are similar without accessing the elements (or index). So sir if you can tell me about this it would be really great.. and Thanks for the videos.
Interviewer:->consider a simple class which has the following structure:- class A { int i; } and when you do, A obj1=new A();.... HOW MANY OBJECTS ARE CREATED FROM the statement above..? I answered 1 , but he said i am wrong... the question was asked in an interview from me..please update with the correct answer ..really hoping to hear from you soon..
We can not use string as for each loop in java but u can do this String name = 'thfhsj' for (int i = 0; i< name.lenght(); i++) { System.out.println(name.indexOf(i)) }
What happens when an object is deleted (garbage collected). How do we decrement the "i" variable to keep accurate track of the CURRENT number of objects ?
package com.amatuer.java; public class CountObjects { public static void main(String[] args) { // Everytime we create an object we call one method which is constructor // In this case the Constructor is "Counter" Counter obj1 = new Counter(); // This will increment the value of i by 1 // This new obj creates an instance in heap and holds the value 1 Counter obj2 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again Counter obj3 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again Counter obj4 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again Counter obj5 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again // The reason being "i" is defined as an instance variable. // in order to hold the same value of "i" in every obj creation that whould be defined as static // Static/Class variable is a variable which shared among the objects obj5.Show(); } } class Counter { static int i; public void Counter() { i++; } public void Show() { System.out.println("Number of objects in class CountObjects is " +i); } }
See count the number of objects first Then put a if statement inside the class that if number of objects is more than two u have to make something that will create an error like there is an array of 5 elements and u will try to get 9th element of that and that will stop the user
how to reverse a given input string could you please explain with an example. please make video on Collectonsframwork I don't able to remember these concept even when read many times
hii naveen , you told that constructor do not work on static variable .. But here i variable is static and then we are incrementing the value of that static variable (i) inside the constructor . How ????
Actual use of static block : We can assign values (initial values) to static variables inside the static block similar to the way we assign values to non-static variables inside constructor. We can also assign values to static variables from constructor. But, the value will be assigned only after the first object of the class is created. And the same code (assignment statement) will be executed unnecessarily every time we create an object of that class. But here, we want to increment the static variable every time we create an object. So we give the increment statement inside the constructor.
I think you want to say the common algorithm jVM follows while allocating memory for the object in the heap when it calls the constructor(basically default) ,I know how to know the approx size.There is a way we can find the total size of object in writing one file in the classpath of the web application mainly in Batch Processing Application.Never say its not possible,Before saying not possible try to inspect got it.Plz go through the J2SE api.thnx
it works for only objects created by default constructor , if we create object by using param-const() it will not work , in that it is showing only the count of default constructor created objects only public class CountObject { static int i; public CountObject() { i++; } CountObject(int i ) { System.out.println("came to param"); // here also we need to increment i value if obj created by parameter-constructor } public static void main(String args[]) { CountObject obj1 = new CountObject(); CountObject obj2 = new CountObject(); CountObject obj3 = new CountObject(10); System.out.println("No. of Objects: " + i); } } here I created 3 objects but it showing count 2 only
package CountObj; public class CountObjects { public static void main(String[] args) { // @author Khamdamboy Urunov 25/08/2016 Acount obj1 = new Acount(); obj1.Countvalue(); Acount obj2 = new Acount(); obj2.Countvalue(); Acount obj3 = new Acount(); obj3.Countvalue(); } } class Acount{ /* * Instance variable is every time update copying value * that means i =0; next creating value also copying this value i=0; * */
static int i; /* * instance variable when using variable without keyword static * we can use class variable keyword static.... * */ public Acount(){ i++; } public void Countvalue() { System.out.println("Number of obj: " +i); } }
First of all you are very beautyful and you can take my code : )class E { public static void main(String args[]) { F obj=new F(); F obj2=new F(); F obj3=new F(); F obj4=new F(); obj4.counter(); } } class F { static int i; //zero public F() { i++; } public void counter() { System.out.println(i); } }
This should work: public class A { static int i; public A() { i++; } public void counter() { System.out.println(i); } public static void main(String[] args) { A obj1 = new A(); A obj2 = new A(); A obj3 = new A(); obj3.counter(); } }
Sir, I am a CSE final year student. I am learning Java from your videos. I feel very much comfortable to learn from your lectures and practical demos. Thank you so much for the clear explanations and apt examples. I have a book titled 'How to crack Interviews in Java' written by Rashmi Kanta Das. It has only questions and one word answers without explanations. Can you please upload videos with explanations for the answers in that book?
Thanks for the answer sir, I was asked this question in my technical interview and you explained it very well😘
Excellent tutorial sir. this is what I had been searching for so many days. Perfect explanation and you explain so nicely that anyone who are really new to Java can also understand. This is a real compliment from my heart. Keep up the work. You are doing a great job helping out young engineers like us learn and get ourselves mastered with Java.
So true........
we want more interview questions
I became your fan and the way of your e explanation is awesome.keep it up
Best java teacher in the world.
This type of questions is very important and help me to understand the concept
Please upload more videos like this thank you 🙏🙏
sir give static that will not replace the value of i .....thank u so much ur videos are in proper order sir
U r just an amazing person in this whole world!
Hi Naveen Sir
I have a few questions for you so could you please answer them through your tutorials? I am certainly not judging your knowledge in this subject but Just requesting you.
1. Create a method which can take any kind of generic parameter.
For instance: map, map, List.
2. Can we use comparable in the catch block ? if so, then what would be the scenario?
3. Can we use Object class in place of Throwable as it is a parent class?
4. What the are the fastest map used in order to store billions of data?
Regards
One Mistake: Sir, You changed 'i' as static variable. So that can't be used in non static method sir. We have to put static in that class method sir.
Otherwise all your videos are really awesome sir. I completed your Python tutorial. Now I'm going with Java. After Java Script. Thank you so much for your videos sir.
That is not a non static method.That is a constructor. Public A() is a constructor.And constructor loads everytime an object is in play.
If you are talking about that counter method which is non static then you are wrong.A non static method can access static variables or class variables.The vice versa is not possible.
we can use static variable in non-static method if i am not wrong.
dear sir, i have a doubt.. please clear
variable 'i', being a non-static variable can't be used inside the count() as well as main() (as you told in the previous lectures), so it is obvious that it will not be compiled at first place and show error if we not make it static
thanks for all your efforts.... great teaching sir
i found it if we have parameterized constructor or method we have to declare i++ inside of that method also to get exact count
Exactly what I needed
Heartly speaking a big thank you.
We want more interview questions sir
We want more interview questions,coding questions,and mcqs(wer we must find Java output)
public class A
{
static int i=0;
A()
{
i++;
}
public static void main(String[] args)
{
A obj0=new A();
A obj1=new A();
A obj2=new A();
A obj3=new A();
System.out.println("Number of objects created:" +i);
}
}
Please create more important and complex interview questions
this is so complex. simply create the anonymous block.
{
i++; // if you do variation i--// you will -as prefix
}
done.
good question show more like this....
There is some simple mistakes in code.
1) public static void counter ( instead of just public void counter)
2) in main A.counter();
(Last line instead of A obj.counter(); )
Thanks 🙂
but we can use a static variable inside a non static method
Thanks for the answer sir
Thanks for sharing your knowledge, I find these videos fun and entertaining.
To became a Java developer ,
Just core Java is enough,or
We should know even advance Java like Jsp servlets , spring MVC
Hi Naveen, can you please create series for UI Automation and Rest API Automation for QA
Thank you sir!! This was very well explained and helpful!
we can simply use instance of and then count
Greatly explained!!
Nice explanation
Love this video!
that's amazing thank you so much!!!
sir
in one of ur video "use of static block" u said constructor don't work on static variables but in this video its contradictory........
plz explain this.....
waiting for an early explanation....
Actual use of static block :
We can assign values (initial values) to static variables inside the static block similar to the way we assign values to non-static variables inside constructor. We can also assign values to static variables from constructor. But, the value will be assigned only after the first object of the class is created. And the same code (assignment statement) will be executed unnecessarily every time we create an object of that class.
yes please make more videos on interview questions
Hello Reddy i think you used to watch Nalamothu Sridhar videos, Because he is also same, In 2 mins with out any head and tail wants to say something.
Yet you should "somehow" avoid/restrict access to your counter variable else one might simply change it back to 0 (A.i = 0) ;^) .
// INTERVIEW QUESTION :- HOW TO KNOW HOW MANY OBJECTS ARE CREATED FOR AN CLASS?
// SOLUTION: THERE IS AN CODE WHICH WILL FIND HOW MANY OBJECTS ARE CREATEING FOR TARGET CLASS.
public static void main(String[] args) {
target obj1=new target();//create object1
target obj2=new target();//create object2
target obj3=new target();//create object3
target obj4=new target();//create object4
target obj5=new target();//create object5
target obj6=new target();//create object6
target obj7=new target();//create object7
obj2.counter();
}
}
class target
{
public static void main(String[] args) {
}// main method under this target class
static int c; // class variable (by default any integer has value 0)
public target() // constructer(which is create by default when we make any new object)
{
c++;//use to count
}
public void counter() // static use because the class variable is static
{
System.out.println(c);//print the counted value
}
}
hi, Telusko , is there a way to use reflection to grt the count? here it is going to only show the instances created, but what abt instances no longer present because of GC.
well explained!! thank you
Hey Navin ,it's really nice yeah
I love you bro
Sir can u do a video on up casting and down casting
amazing.
Sir i have a question
how can we do operation with a static member in a non static method? As you have done with 'i' variable.
actually I wanted to ask the same
Raounaq Sharma bro we can have a static variable inside a non-static method but it is necessary to have a static variable with static method..i hope this will clear your doubt
That's a constructor and there is nothing like static constructor.
Inside non static method we can use static variable..i.e [public void counter()] is correct....but we can't use static variable inside a constuctor... right?
@@manasranjan5232 i think so
Please do create a project on java.
hello sir i really like your videos and they are very simple to understand also.....but sir i have recently given TCS interview and they asked me a question about an array ...How will you tell that the two array are similar without accessing the elements (or index). So sir if you can tell me about this it would be really great.. and Thanks for the videos.
Convert both to string and use .equals or compare to
Interviewer:->consider a simple class which has the following structure:-
class A
{
int i;
}
and when you do, A obj1=new A();....
HOW MANY OBJECTS ARE CREATED FROM the statement above..?
I answered 1 , but he said i am wrong...
the question was asked in an interview from me..please update with the correct answer ..really hoping to hear from you soon..
None is this correct?
Question 1 : How to use String with "for loop", not for each loop?
Question 2: How to use String with the Switch statement?
Thanks
We can not use string as for each loop in java but u can do this
String name = 'thfhsj'
for (int i = 0; i< name.lenght(); i++)
{
System.out.println(name.indexOf(i))
}
No need to create objects for static variable right sir?
incredible!
superb
Plz sir upload this type of interesting codes
What about the parameterized constructor?
What happens when an object is deleted (garbage collected). How do we decrement the "i" variable to keep accurate track of the CURRENT number of objects ?
Sir can you plz explain this practically just by doing a program of it
what if we are calling the constructor using super() or this(). No new object is created still value of counter will increment.
thanx brother (y)
package com.amatuer.java;
public class CountObjects {
public static void main(String[] args)
{
// Everytime we create an object we call one method which is constructor
// In this case the Constructor is "Counter"
Counter obj1 = new Counter(); // This will increment the value of i by 1
// This new obj creates an instance in heap and holds the value 1
Counter obj2 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again
Counter obj3 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again
Counter obj4 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again
Counter obj5 = new Counter(); // This new obj creates an instance in heap again and holds the value 1 again
// The reason being "i" is defined as an instance variable.
// in order to hold the same value of "i" in every obj creation that whould be defined as static
// Static/Class variable is a variable which shared among the objects
obj5.Show();
}
}
class Counter
{
static int i;
public void Counter()
{
i++;
}
public void Show()
{
System.out.println("Number of objects in class CountObjects is " +i);
}
}
Interview ques : How do i stop a user from creating more than two objects of a class ?
Answer please
See count the number of objects first
Then put a if statement inside the class that if number of objects is more than two u have to make something that will create an error like there is an array of 5 elements and u will try to get 9th element of that and that will stop the user
Good query, i will try it out tomorrow.
sir, can you give us some more important questions for interview.
how to reverse a given input string could you please explain with an example.
please make video on Collectonsframwork I don't able to remember these concept even when read many times
Class A and variable I should be static in order to count the number of objects.
Make more interview questions
Sir how many objlect should be created from one class please tell me or make vedio on it
hii naveen , you told that constructor do not work on static variable .. But here i variable is static and then we are incrementing the value of that static variable (i) inside the constructor . How ????
Actual use of static block :
We can assign values (initial values) to static variables inside the static block similar to the way we assign values to non-static variables inside constructor. We can also assign values to static variables from constructor. But, the value will be assigned only after the first object of the class is created. And the same code (assignment statement) will be executed unnecessarily every time we create an object of that class.
But here, we want to increment the static variable every time we create an object. So we give the increment statement inside the constructor.
Sir plz upload a video regarding how to get the size of object in java
can you tell me how
I think you want to say the common algorithm jVM follows while allocating memory for the object in the heap when it calls the constructor(basically default) ,I know how to know the approx size.There is a way we can find the total size of object in writing one file in the classpath of the web application mainly in Batch Processing Application.Never say its not possible,Before saying not possible try to inspect got it.Plz go through the J2SE api.thnx
stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object follow this and stop saying impossible
Have u worked on JMon APi
sir ,according to your method i have to add " i++" in every constructor . Is there any other way
Sandeep Rawat but you need only 1 constructor
can a static variable be used in a Constructor ??
What is I make copy of one object to another object?Without calling the construction.
I want hashmap internals pl teach it and Also sorting and data structures
What if parameterized constructor in creation..?
Sir do you have Android lectures? I am also ready to subscribe if you can give Android classes.
but what to do if i have a parametrized constructor??
Hi .can u tell me how to count the number of lines of a function
but what is the class have multiple overloaded constructors ?
how to decrease that variable when object destroyed?
Sir can we make counter method as static ?
yes, i tried it
Code :
public class ObjectCount {
public static void main(String[] args) {
Abc5.dispCount();
Abc5 obj1=new Abc5();
Abc5 obj2=new Abc5();
Abc5 obj3=new Abc5();
obj1.dispCount();
Abc5 obj4=new Abc5();
Abc5 obj5=new Abc5();
Abc5.dispCount();
Abc5 obj6=new Abc5();
Abc5 obj7=new Abc5();
Abc5 obj8=new Abc5();
Abc5 obj9=new Abc5();
obj9.dispCount();
Abc5 obj10=new Abc5();
Abc5 obj11=new Abc5();
Abc5 obj12=new Abc5();
Abc5.dispCount();
}
}
class Abc5
{
static int count=0;
Abc5()
{
count++;
}
static void dispCount()
{
System.out.println("No. of objects of Abc5 class ="+count);
}
}
it works for only objects created by default constructor , if we create object by using param-const() it will not work , in that it is showing only the count of default constructor created objects only
public class CountObject
{
static int i;
public CountObject()
{
i++;
}
CountObject(int i )
{
System.out.println("came to param");
// here also we need to increment i value if obj created by parameter-constructor
}
public static void main(String args[])
{
CountObject obj1 = new CountObject();
CountObject obj2 = new CountObject();
CountObject obj3 = new CountObject(10);
System.out.println("No. of Objects: " + i);
}
}
here I created 3 objects but it showing count 2 only
Plese upload more videos
thanks alote..........
why main class is kept as static??
And how main class is called without creating its object??
Yashasvi Shukla you just answer your own question ...since main is static it does not require object creation to access variable or methods
Why it will print 3 if it calls obj2?
obj2.counter() will print 3 because all the objects are created before calling counter() method.
❤️
I hope you know telugu because you said you are south indian. Is it rightl?
👍
package CountObj;
public class CountObjects {
public static void main(String[] args) {
// @author Khamdamboy Urunov 25/08/2016
Acount obj1 = new Acount();
obj1.Countvalue();
Acount obj2 = new Acount();
obj2.Countvalue();
Acount obj3 = new Acount();
obj3.Countvalue();
}
}
class Acount{
/*
* Instance variable is every time update copying value
* that means i =0; next creating value also copying this value i=0;
* */
static int i; /*
* instance variable when using variable without keyword static
* we can use class variable keyword static....
*
*/
public Acount(){
i++;
}
public void Countvalue()
{
System.out.println("Number of obj: " +i);
}
}
Okay. What happens when we create an anonymous class...
Can anyone give me full program for this...
how to count total mobiles present on shopping websites????????????????/
Hey,
Can you please share the code for this as I am not able to get an output.
class CountObject
{
static int i;
public CountObject()
{
i++;
}
public static void main(String args[])
{
CountObject obj1 = new CountObject();
CountObject obj2 = new CountObject();
System.out.println("No. of Objects: " + i);
}
}
//Output : No. Of Objects: 2
First of all you are very beautyful and you can take my code : )class E
{
public static void main(String args[])
{
F obj=new F();
F obj2=new F();
F obj3=new F();
F obj4=new F();
obj4.counter();
}
}
class F
{
static int i; //zero
public F()
{
i++;
}
public void counter()
{
System.out.println(i);
}
}
pls give us the code
This should work:
public class A {
static int i;
public A()
{
i++;
}
public void counter()
{
System.out.println(i);
}
public static void main(String[] args) {
A obj1 = new A();
A obj2 = new A();
A obj3 = new A();
obj3.counter();
}
}
output will be 3
I don't understand this video!