I'll focus my answer on why you want to do that, and if it's an effective/safe way to achieve the goals that you are truly seeking (because even if you can increase your bone density, this does not necessarily mean that you achieve the goal you want).
I'll work with 3 scenarios:
I want to hit my opponent harder
First of all, you should focus on learning and executing the correct technique. You can have all the bone density you want, but a correctly executed strike will be much more effective and "harder" than a poorly executed strike. Training to achieving speed and correct technique should be much more important in your training routine than increasing your bone density.
Secondly, it's not the weight or the strength of your bones that results in a powerful punch, but the body weight, technique, and MUSCLE. If you have 10 kg of muscle on your arm, you may be able to hit a wall after years of training and not break your hand. However, if I have 30 kg of muscle on my arm, I can knock you out without the tedious and painful time spent hitting a wall. If you want to hit harder, hit the gym, build some muscle, and learn how to use your body weight in your favor.
Thirdly, know where to hit, and how to use speed and body weight to hit gracefully. Learning where it hit is down to you, your instructor, and your martial art, so I won't expand on this much. You don't need super strength to hurt (or even kill) someone; you just need to know where to hit them.
A few more tips:
Your strongest punch doesn’t land when your arms are fully out-stretched; your punch hits harder when it lands a bit shorter than your full range of motion.
Getting hit by a counter-punch hurts more than anything else.
Exhale sharply on every punch.
Body rotation, body rotation, body rotation. A full rotation with short arm extension hits harder than a small rotation with full arm extension.
In conclusion, hitting a wall or other hard, immovable object, doesn't make any sense if your goal is to "hit harder"; the ability to hit hard is the result of good technique, having muscle, creating impulse and speed, and knowing where to hit. Remember that essentially, mass (m) times (v) velocity equals impact power.
I want to feel less pain
There is already a very good question about this here on Martial Arts. Indeed one of the symptoms of osteoporosis is pain. But the bones are not where you feel pain. You feel pain on the nerves. And pain is not a physical thing, it happens only inside your head. Control your mind and you control your pain.
At same time, you can become desensitized to the pain if it's regular; your body learns to ignore it and you feel less and less pain, and at same time it grows stronger, not just the bones, but the nerves, the muscles, everything!
From personal experience, I had a lot of pain when practising some Krav Maga moves, especially immobilization, but I kept training and every day the pain decreased. Was it because of increased bone density? I doubt it, because it's an exercising with minimal direct impact on your bones (can't say the same for your joints). I only become desensitized about the pain, and acquired more flexibility so the range of motion didn't hurt as much.
There is little research on this subject, so I can't prove that increased bone density means less pain, but even if they researched this it would be difficult to prove that isn't just desensitization or muscle adaptation.
So, the best I can say to you is if you want to feel less pain, then feel the pain! Feel the pain with all your being, feel it on every cell, recognize that it's merely a warning of your body and that it will pass eventually.
While the same exercises that supposedly increase your bone density are the same that will help you to overcome the pain, it isn't possible to prove a direct link between the two. It would be very hard for any scientist to isolate the conditions and the biological adaptations that occur at all levels (plus pain can't be really measured).
So, just pick something to hit and keep hitting it, or do whatever is causing you pain and do it, but don't go to extremes thinking that opening micro-holes in your bones will help you in anything and give you time to rest! You will see that eventually the pain will decrease (if you have any medical condition, or are feeling extreme pain while or after doing this, stop immediately and seek professional help; you might have gone too far).
I want to prevent broken bones
This one is tricky. While increased bone density make bones stronger, it also make them less flexible. So it's a question of balance. Too low bone density and your bones will crack like glass; too much bone density and your bones cannot bend and will crack too.
The scientific evidence on low bone density and broken bones is very well documented . The scientific evidence on high density and fractures is also very well documented; when the bone density is very high, it is usually associated with some sort of disease or illness and bone density is not equal to bone strength ( and ).
The scientific evidence on the risks of punching hard objects for a long time is scarce and as such, I cannot guarantee that it is safe or unsafe, or effective at all. So, why risk it?
However, I would reiterate that the majority of people break bones punching because of bad technique. My instructor reminds us all of the time of the importance of having good technique to avoid breaking your hand; some people above my grade already broke their hands because they were doing it wrong.
Technique is always everything! If in doubt, ask your instructor how to punch properly and practice that, repeatedly. This is a much better advice to prevent broken bones than "pick a wall, put some news papers, and hit it 20 times every day" because no matter how strong your bones are, if you are punching the wrong way, you will break your hand.
You may also be interested to see what you can do to strengthen your hands and wrists with weights; you may be surprised how weak your hands, fingers, and wrists are if you have never worked them before. Hand and wrist exercises really helped me with my martial arts, possibly more effectively and safely than hitting a wall. I won't include the specific exercises here, because my answer is already very long. I hope this helps and answers what you truly wanted to know behind your question.